Java Reference Assignment and Null Handling

Utilizing Java Object-Oriented Approach
Answer Correct answer: B — The program prints "Cat Cat" because both references point to the same modified object, and nullifying one does not affect the other.

Given the code fragment: What is the result? - image

  1. Cat -
  2. Cat - Correct Answer
  3. Dog -
  4. A NullPointerException is thrown.
  5. Cat -

Community Votes

B
100%

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)

SrinivasJasti 👍 1 Selected: B
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
9817c20 👍 1
Yep its B!
xplorerpj 👍 1 Selected: B
B is correct answer
james2033 👍 2 Selected: B
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

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

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.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide