Object Oriented Programming - I (3140705) MCQs

MCQs of Objects and Classes

Showing 31 to 36 out of 36 Questions
31.
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
Answer:

Option (a)

32.
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 obj = new box();
            System.out.println(obj);
        } 
    }
(a) 0
(b) 1
(c) Runtime error
(d) classname@hashcode in hexadecimal form
Answer:

Option (d)

33.
What will be the output of the following Java code?
    class area 
    {
        int width;
        int length;
        int area;
        void area(int width, int length) 
        {
            this.width = width;
            this.length = length;
        }
 
    }    
    class Output 
    {
        public static void main(String args[])
        {
            area obj = new area();
            obj.area(5 , 6);
            System.out.println(obj.length + "" "" + obj.width);        
        } 
    }
(a) 0 0
(b) 5 6
(c) 6 5
(d) 5 5
Answer:

Option (c)

34.
What will be the output of the following Java program?
    class access
    {
        public int x;
 	static int y;
        void cal(int a, int b)
        {
            x +=  a ;
            y +=  b;
        }        
    }    
    class static_specifier 
    {
        public static void main(String args[])
        {
            access obj1 = new access();
            access obj2 = new access();   
            obj1.x = 0;
            obj1.y = 0;
            obj1.cal(1, 2);
            obj2.x = 0;
            obj2.cal(2, 3);
            System.out.println(obj1.x + "" "" + obj2.y);     
        }
   }
(a) 1 2
(b) 2 3
(c) 3 2
(d) 1 5
Answer:

Option (d)

35.
What will be the output of the following Java program?
    class access
    {
       static int x;
       void increment()
       {
           x++;
       }   
     }   
    class static_use 
    {
        public static void main(String args[])
        {
            access obj1 = new access();
            access obj2 = new access();
            obj1.x = 0;   
            obj1.increment();
            obj2.increment();
            System.out.println(obj1.x + "" "" + obj2.x);
         }
   }
(a) 1 2
(b) 1 1
(c) 2 2
(d) Compilation Error
Answer:

Option (c)

36.
What will be the output of the following Java program?
    class static_out 
    {
        static int x;
 	static int y;
        void add(int a , int b)
        {
            x = a + b;
            y = x + b;
        }
    }    
    class static_use 
    {
        public static void main(String args[])
        {
            static_out obj1 = new static_out();
            static_out obj2 = new static_out();   
            int a = 2;
            obj1.add(a, a + 1);
            obj2.add(5, a);
            System.out.println(obj1.x + "" "" + obj2.y);     
        }
   }
(a) 7 7
(b) 6 6
(c) 7 9
(d) 9 7
Answer:

Option (c)

Showing 31 to 36 out of 36 Questions