Skip to content
Related Articles

Related Articles

Java | Class and Object | Question 6
  • Difficulty Level : Hard
  • Last Updated : 12 Jan, 2016

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :