QuestionQ38

Java Object-Oriented Approach

Given the following code fragment:

Question Image

What is the output?

  • A Cat -Dog
  • B Cat -Cat
  • C Dog -Dog
  • D A NullPointerException is thrown.
  • E Cat -null
Explanation

Both variables reference the same Pet object, so assigning "Cat" through p1.name updates the object's name. Assigning null to p does not affect p1, which still refers to that object; therefore, both printed values are Cat.

Community Discussion

No comments yet. Be the first to start the discussion!