Java OCA OCP Practice Question 2901

Question

Given:

2. public class Main extends Shape {  
3.   public static void main(String[] args) {  
4.     new Main();  
5.     new Main("Tigger");  
6.   }  // w w w  . j a v  a  2s . co m
7.   Main() { this("Pooh"); }  
8.   Main(String s) { super(s); }  
9. }  
10. class Shape {  
11.   Shape(String s) { System.out.print(s + " "); }  
12. } 

What is the result?

  • A. Pooh Pooh
  • B. Pooh Tigger
  • C. Tigger Pooh
  • D. Tigger Tigger
  • E. Compilation fails due to a single error in the code.
  • F. Compilation fails due to multiple errors in the code.


B is correct.

Note

The no-arg Main constructor invokes the other Main constructor (via "this"), and the second Main constructor chains to the Shape's constructor (via "super").




PreviousNext

Related