Java OCA OCP Practice Question 1539

Question

What is the output of the following application?

package mypkg; //from   w  w  w . ja  v  a2s  . c  o m
  
class Exception1 extends Exception {} 
abstract class Printable { 
   protected abstract void isPrintable() throws Exception1; 
} 
public class Main extends Printable { 
   protected void isPrintable() throws Exception {  // m1 
      throw new RuntimeException(); 
   } 
   public static void main(String[] will) throws Throwable {  // m2 
      new Main().isPrintable();  // m3 
   } 
} 
  • A. The code does not compile because of line m1
  • B. The code does not compile because of line m2.
  • C. The code does not compile because of line m3.
  • D. The code compiles but throws an exception at runtime.


A.

Note

The code does not compile because the declaration of isPrintable() in the class Main is an invalid method override.

An overridden method may not throw a broader checked exception than it inherits.

Since Exception is a superclass of Exception1, thrown by the inherited method in the Printable class, the override of this checked exception is invalid.

For this reason, line m1 does not compile, and Option A is the correct answer.

The rest of the lines of code compile without issue.




PreviousNext

Related