Java | Class and Object | Question 4

filter_none

edit
close

play_arrow

link
brightness_4
code

class demo
{
    int a, b;
      
    demo()
    {
        a = 10;
        b = 20;
    }
      
    public void print()
    {
        System.out.println ("a = " + a + " b = " + b + "\n");
    }
}
  
class Test
{
  
    public static void main(String[] args)
    {
        demo obj1 = new demo();
        demo obj2 = obj1;
  
        obj1.a += 1;
        obj1.b += 1;
  
        System.out.println ("values of obj1 : ");
        obj1.print();
        System.out.println ("values of obj2 : ");
        obj2.print();
  
    }
}

chevron_right


(A) Compile error
(B)

values of obj1: 
a = 11 b = 21
values of obj2: 
a = 11 b = 21

(C)

values of obj1: 
a = 11 b = 21
values of obj2: 
a = 10 b = 20

(D)



values of obj1: 
a = 11 b = 20
values of obj2: 
a = 10 b = 21

(E) Run time error


Answer: (B)

Explanation: Assignment of obj2 to obj1 makes obj2 a reference to obj1. Therefore, any change in obj1 will be reflected in obj2 also.

Quiz of this Question
Please comment below if you find anything wrong in the above post



My Personal Notes arrow_drop_up