Java Reference Assignment and Null Handling
Short answer
B The program prints "Cat Cat" because both references point to the same modified object, and nullifying one does not affect the other.
Question

Key Takeaways
The trap is confusing variable reassignment with object mutation; assigning a new value to a variable changes its reference, but changing an object's field via a reference persists for all references pointing to that object.
This question tests Java reference assignment behavior, specifically how multiple variables can point to the same object instance. The correct answer is B because modifying the object through one reference affects all references, and nullifying one does not affect the other.
Common mistake: Candidates often choose C or D, mistakenly believing that 'p = p1' creates a deep copy or that setting 'p = null' breaks the link between 'p' and 'p1', failing to understand they share the same heap memory address.
Expert Analysis
Why the Answer Is Correct
The code initializes a static String name in the Pet class. First, p is created with name "Dog". Then p1 = p, so both p and p1 refer to the exact same object instance on the heap. When p1.name = "Cat" is executed, it modifies the shared object's state. Consequently, p.name also becomes "Cat" because p points to the same object. The line p = p1 is redundant as they already point to the same object. Finally, p = null only sets the local variable p to null; it does not affect p1, which still holds a valid reference to the "Cat" object. Therefore, System.out.println(p.name) prints "Cat", and System.out.println(p1.name) also prints "Cat".
Why the Other Options Are Wrong
Option A ("Dog - Dog") is incorrect because the name was explicitly changed to "Cat" via p1. Option C ("Cat - Dog") suggests that p and p1 are independent objects after modification, which ignores the fact that p1 = p makes them aliases. Option D (NullPointerException) is wrong because although p is set to null, the second print statement uses p1, which is not null. Option E is identical to B in text but likely represents a formatting distractor or duplicate; since B is the standard format for the correct output, B is selected.
Community Comment Notes
As user james2033 noted, the key is that p and p1 reference the same object, so changing the name via one reflects in the other. SrinivasJasti confirmed that setting p = null leaves p1 intact, leading to the "Cat Cat" output. Multiple users voted for B, agreeing that the shared reference logic is the core concept tested here.
Exam Strategy
Always trace object references: if two variables are assigned to each other (b = a), they point to the same memory location. Modifying the object through one variable changes it for all. Setting a variable to null only affects that specific variable, not the object itself or other variables pointing to it.
Frequently Asked Questions
Does p = null affect p1?
Why does p.name change when p1.name is modified?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 4 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 4
-
james2033 👍 2 Answered Bpackage 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
-
SrinivasJasti 👍 1 Answered Bp 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
-
xplorerpj 👍 1 Answered BB is correct answer
-
9817c20 👍 1Yep its B!
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.