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








Question

What is the output of the following code? (Choose all that apply)

     1: interface Printable { 
     2:   public default int getPageNumber(int input) { return 2; } 
     3: } 
     4: public class Shape implements Printable { 
     5:   public String getPageNumber() { return "4"; } 
     6:   public String getPageNumber(int input) { return "6"; } 
     7:   public static void main(String[] args) { 
     8:     System.out.println(new Shape().getPageNumber(-1)); 
     9:   } 
     10: } 
  1. 2
  2. 4
  3. 6
  4. The code will not compile because of line 5.
  5. The code will not compile because of line 6.
  6. The code will not compile because of line 8.




Answer



E.

Note

The code doesn't compile because line 6 contains an incompatible override of the getPageNumber(int input) method defined in the Printable interface.

int and String are not covariant returns types, since int is not a subclass of String.

line 5 compiles without issue; getPageNumber() is an overloaded method that is not related to the parent interface method that takes an int value.