Open In App

Java | Class and Object | Question 4




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

(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



Article Tags :