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








Question

What is the output of the following code?

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




Answer



B.

Note

This code compiles and runs without issue, outputting false.

The first declaration of isPrinted() is as a default interface method, assumed public.

The second declaration of isPrinted() correctly overrides the default interface method.

Shape instance may be automatically cast to a Printable reference without an explicit cast, although adding it doesn't break the code.