Java OCA OCP Practice Question 143

Question

Given:

3. class MyBaseClass { 
4.   static String s = " "; 
5.   protected MyBaseClass() { s += "alpha "; } 
6. } /*  w  w w.j  a v a 2  s .co  m*/
7. class MyClass extends MyBaseClass { 
8.   private MyClass() { s += "sub "; } 
9. } 
10. public class MyClass2 extends MyBaseClass { 
11.   private MyClass2() { s += "subsub "; } 
12.   public static void main(String[] args) { 
13.     new MyClass2(); 
14.     System.out.println(s); 
15.   } 
16. } 

What is the result?

A.   subsub 
B.   sub subsub 
C.   alpha subsub 
D.   alpha sub subsub 
E.   Compilation fails 
F.   An exception is thrown at runtime 


C is correct.

Note

MyClass2 extends MyBaseClass!

Since the code doesn't attempt to make a MyClass, the private constructor in MyClass is okay.




PreviousNext

Related