Java OCA OCP Practice Question 1231

Question

What is the output of the following application?

package mypkg; //w w w . jav a2 s. c  o m

class Shape { 
        public long c; 
        public Shape(long c) { 
           this.c = c+2; 
        } 
} 


public class Rectangle extends Shape { 
        public Rectangle(long c) { 
           this.c = c+1; 
        } 
        public static void main(String[] birds) { 
           System.out.print(new Rectangle(5).c); 
        } 
} 
  • A. 5
  • B. 6
  • C. 8
  • D. The code does not compile.


D.

Note

Since a constructor call is not the first line of the Rectangle() constructor, the compiler inserts the no-argument super() call.

Since the parent class, Shape, does not define a no-argument super() constructor, the Rectangle() constructor does not compile, and Option D is correct.




PreviousNext

Related