Java Reference Assignment and Null Handling
Given the code fragment: What is the result? - 
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (4 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The code initializes a static Stringname 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" viap1. 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 thatp 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?
No. p = null only sets the variable p to null. p1 still holds a valid reference to the original object.
Why does p.name change when p1.name is modified?
Because p1 = p makes both variables point to the same object instance in memory. Changes to the object are visible through any reference.