Java OCA OCP Practice Question 2459

Question

Given:

3. public class Main extends Car {  
4.   Main() { System.out.println("r "); }  
5.   public static void main(String[] args) {  
6.     new Main();  
7.   }  /*from   w ww .  j  a va 2  s  . c o  m*/
8. }  
9. class Car extends Shape {  
10.   Car() { System.out.print("c "); }  
11.   private Car(String s) { }  
12. }  
13. abstract class Shape {  
14.   Shape() { System.out.print("s "); }  
15. } 

What is the result?

  • A. r
  • B. c r
  • C. r c
  • D. s c r
  • E. r c s
  • F. Compilation fails due to a single error in the code.
  • G. Compilation fails due to multiple errors in the code.


D is correct.

Note

It's legal for abstract classes to have constructors, and it's legal for a constructor to be private.

Normal constructor chaining is the result of this code.




PreviousNext

Related