Java | Class and Object | Question 6

Predict the output of the following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
  
    }
}

chevron_right


(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



My Personal Notes arrow_drop_up