Java OCA OCP Practice Question 3004

Question

Given:

2. class Shape {  
3.   private Shape() { }  
4.   protected Shape(int x) { }  
5. }  // ww  w .ja  v  a 2  s.  co m
6. public class Main extends Shape {  
7.   // Main(int x) { }       
8.   // Main(int x) { super(); }     
9.   // Main(int x) { super(x); }     
10.   // private Main(int x) { super(x); }  
11.   // Main() { }           
12.   // Main() { this(); }     
13.   // Main() { this(6); }    
14.   // Main() { super(7); }    
15.   public static void main(String[] args) {  
16.     new Main(7);   
17.     new Main();    
18. } } 

Which sets of constructors can be uncommented for the code to compile? (Choose all that apply.)

  • A. Line 7 and line 11.
  • B. Line 7 and line 14.
  • C. Line 8 and line 12.
  • D. Line 8 and line 13.
  • E. Line 9 and line 12.
  • F. Line 9 and line 13.
  • G. Line 10 and line 11.
  • H. Line 10 and line 14.


F	 and H are correct.

Note

None of the constructors can chain to Shape's private constructor, they must all get to Shape's protected constructor somehow.

Lines 7 and 8 won't ever work because Shape's protected constructor needs an argument.

Line 13 would work with either line 9 or line 10.

Line 14 would work with either line 9 or line 10.




PreviousNext

Related