In Java, consider the following classes and methods: class Value { public int i = 15; } public class Test { public static void main(String argv[]) { Test t = new Test(); t.first(); } public void first() { int i = 5; Value v = new Value(); v.i = 25; second(v, i); System.out.println(v.i); } public void second(Value v, int i) { i = 0; v.i = 20; Value val = new Value(); v = val; System.out.println(v.i + " " + i); } } What output is produced when this program is compiled and run?

Difficulty: Medium

Correct Answer: 15 0 20

Explanation:


Introduction / Context:
This Java question probes understanding of how Java passes arguments to methods, especially object references versus primitive values. Many learners confuse Java with languages that use pass by reference semantics. In Java, method arguments are always passed by value, including object references, which leads to behaviour that can be surprising at first when objects are reassigned inside methods.


Given Data / Assumptions:

  • A class Value has a public integer field i initialised to 15.
  • The first method in the Test class declares an int i and a Value object v.
  • Inside first, v.i is set to 25 and then second is called with v and i.
  • Inside second, the local int parameter i is set to 0, and v.i is set to 20.
  • A new Value object val is created and assigned to local variable v, and then values are printed.


Concept / Approach:
Java passes object references by value. This means the method receives a copy of the reference, not a direct alias to the variable in the caller. Changing fields through the reference affects the shared object, but reassigning the local reference to a new object does not affect the caller. Primitive types such as int are also passed by value, so changes to parameters do not propagate back to the caller. Tracking which object each reference points to at each step allows us to determine what is printed.


Step-by-Step Solution:
Step 1: In first, local i is 5. A new Value object is created, its field i is set to 25, and the reference v points to this object. Step 2: The call second(v, i) passes the reference to the Value object and the primitive value 5 by value. Step 3: Inside second, local parameter i is set to 0. The parameter v still refers to the original Value object. Setting v.i to 20 changes that object field from 25 to 20. Step 4: A new Value object val is created with its field i initialised to 15. Then the local reference v is reassigned to refer to val. This does not change what v refers to in the first method. Step 5: second prints v.i and i. At this point, v refers to val with i equal to 15, and i is 0, so it prints 15 0. Control returns to first, where v still refers to the original Value object whose i field is now 20, so first prints 20. Combining the values, the choices represent this as 15 0 20.


Verification / Alternative check:
If you run this program in a Java environment, you will see two lines printed. The first line from second is 15 0, and the second line from first is 20. Exam style answers often compress these into a sequence such as 15 0 20, which matches option D. This direct execution is a reliable confirmation of the theoretical analysis.


Why Other Options Are Wrong:
Option A, 15 0 2, introduces a value 2 that never appears in the code. Option B, 15 0 0, suggests that the field i of the original object was not set to 20 in second, which is incorrect. Option C, 15 20 0, misorders or misinterprets the printed values. Option E, 20 0 15, assigns wrong values to the printed lines, ignoring the effect of reassigning the local reference v.


Common Pitfalls:
A common mistake is to believe that assigning v = val in second also changes the reference in first, which would be the case if Java used pass by reference semantics. Another pitfall is failing to distinguish between changing an object field through a reference and reassigning the reference itself. Remember that Java passes arguments by value, including object references, and track carefully which variable points to which object at each step.


Final Answer:
The program prints 15 0 from the second method and 20 from the first method, which is represented by the option 15 0 20.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion