Java | Class and Object | Question 7
Predict the output of the following program.
class Test { int a = 1 ; int b = 2 ; Test func(Test obj) { Test obj3 = new Test(); obj3 = obj; obj3.a = obj.a++ + ++obj.b; obj.b = obj.b; return obj3; } public static void main(String[] args) { Test obj1 = new Test(); Test obj2 = obj1.func(obj1); System.out.println( "obj1.a = " + obj1.a + " obj1.b = " + obj1.b); System.out.println( "obj2.a = " + obj2.a + " obj1.b = " + obj2.b); } } |
(A)
obj1.a = 1 obj1.b = 2 obj2.a = 4 obj2.b = 3
(B)
obj1.a = 4 obj1.b = 3 obj2.a = 4 obj2.b = 3
(C) Compilation error
Answer: (B)
Explanation:
obj1 and obj2 refer to same memory address.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Please Login to comment...