Java OCA OCP Practice Question 2811

Question

Given:

2. public class Main extends MyClass {  
3.   // insert code here  
5.   public static void main(String[] args) {  
6.     new Main("Java");  
7.     new Main();  
8.   }  /*  w  ww .  j  a v a  2  s  . c o m*/
9. }  
10. class MyClass {  
11.   MyClass(String s) { System.out.print(s + " "); }  
12. } 

Which set(s) of code, inserted independently at line 3, produce the output "Java SQL "? (Choose all that apply.).

A.   Main() { this("SQL"); }  
    Main(String s) { super(s); } 

B.   Main() { super("SQL"); }  
    Main(String s) { super(s); } 

C.   Main() { super(); }   
    Main(String s) { super(s); }  

D.   Main() { super("SQL"); }   
    Main(String s) { this(); } 

E.   Main() { super("SQL"); }  
    Main(String s) { this("Java"); }  


H:Note

A and B are two correct ways for Main's constructors to interact with MyClass's constructor.

C will not compile because Main's constructors assume MyClass has a no-arg constructor.

D compiles but produces the output "SQL SQL ".

E will not compile because the one-arg constructor is attempting to call itself recursively.




PreviousNext

Related