OCA Java SE 8 Class Design - OCA Mock Question Class Design 4








Question

What is the output of the following code?

     1: class Shape  
     2:   public void printName(double input) { System.out.print("Shape"); } 
     3: } 
     4: public class Rectangle extends Shape { 
     5:   public void printName(int input) { System.out.print("Rectangle"); } 
     6:   public static void main(String[] args) { 
     7:     Rectangle r = new Rectangle(); 
     8:     r.printName(4); 
     9:     r.printName(9.0); 
     10:   } 
     11: } 
  1. RectangleShape
  2. ShapeRectangle
  3. RectangleRectangle
  4. ShapeShape
  5. The code will not compile because of line 5.
  6. The code will not compile because of line 9.




Answer



A.

Note

The code compiles and runs without issue, so E and F are incorrect.

The printName() method is an overload in Rectangle, not an override, so both methods may be called.

The call on line 8 references the version that takes an int as input defined in the Rectangle class, and the call on line 9 references the version in the Shape class that takes a double.

RectangleShape is output and option A is the correct answer.