What will be the output of the following Java program?

class box 
   {
       int width;
       int height;
       int length;
   } 
   class mainclass 
   {
       public static void main(String args[]) 
       {        
           box obj1 = new box();
           box obj2 = new box();
           obj1.height = 1;
           obj1.length = 2;
           obj1.width = 1;
           obj2 = obj1;
           System.out.println(obj2.height);
       } 
   }

a) 1
b) 2
c) Runtime error
d) Garbage value

1 thought on “What will be the output of the following Java program?”

  1. a
    Explanation: When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator.
    output:
    $ javac mainclass.java
    $ java mainclass
    1

Leave a Comment