Java OCA OCP Practice Question 2479

Question

Given:

2. class MyClass {  
3.   static String s = "-";  
4.   String s2 = "s2";  
5.   MyClass(String arg) { s += arg; }  
6. }  //from   ww  w.j  a  va 2s .c  o  m
7. public class Main extends MyClass {  
8.    Main() { super(s2); }  
9.    { s += "i "; }  
10.   public static void main(String[] args) {  
11.     new Main();  
12.     System.out.println(s);  
13.   }  
14.   static { s += "sb "; }  
15. } 

What is the result?

  • A. -sb i s2
  • B. -sb s2 i
  • C. -s2 i sb
  • D. -s2 sb i
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


E is correct.

Note

The s2 variable is an instance variable that can't be used in the call to super because the instance hasn't been created yet.




PreviousNext

Related