Java OCA OCP Practice Question 2268

Question

How many lines of the following application contain a compilation error?

package mypkg; //from  w  w w.  ja  v a2  s.  com
final interface Printable { 
   default long print() {return 20;} 
} 
abstract class Printer { 
   abstract long print(); 
} 
final class Main extends Printer implements Printable { 
   long print() {return 40;} 
  public static final void main(String[] pictures) { 
     final Printable f = new Main(); 
     System.out.print(f.print()); 
  } 
} 
  • A. One
  • B. Two
  • C. Three
  • D. None. The code compiles and prints 20 at runtime.
  • E. None. The code compiles and prints 40 at runtime.


B.

Note

The code does not compile, so Options D and E are incorrect.

The first compilation error is in the Printable interface declaration.

Since all interfaces are implicitly abstract, they cannot be marked final.

The second compilation error is the declaration of the print() method in the Main class.

Since print() is inherited from the Printable interface, it is implicitly public.

This makes the override of the method in Printable invalid because the lack of access modifier indicates package-level access.

Since package-level access is more restrictive than the inherited method's access modifier, the overridden method does not compile in the Main class.

Option B is the correct answer.




PreviousNext

Related