p and p1 both point to the same Pet object that has the name "Dog" if p1.name changes the p.name also is same as p1.name as they reference same object, so p =p1 in the 4th lines has no significance and when p is set to null the reference of p1 does not change so answer is Cat Cat
package q50;
public class Q50 {
public static void main(String[] args) {
Pet p = new Pet("Dog");
Pet p1 = p;
p1.name = "Cat";
p = p1;
System.out.println(p.name);
p = null;
System.out.println(p1.name);
}
}
class Pet {
public static String name;
public Pet(String name) {
this.name = name;
}
public String name() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// Result:
// Cat
// Cat
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
SrinivasJasti
2 weeks, 1 day ago9817c20
2 months, 1 week agoxplorerpj
6 months, 3 weeks agojames2033
11 months ago