Java OCA OCP Practice Question 645

Question

What is the output of the following application?

package mypkg; /*from  ww  w  .  j  ava  2  s .  c  o m*/
class Printable { 
   public void operate() throws RuntimeException { 
      throw new RuntimeException("Not supported"); 
   } 
} 
public class Main extends Printable { 
   public void operate() throws Exception { 
      System.out.print("beat"); 
   } 
   public static void main(String... cholesterol) throws Exception { 
      try { 
         new Main().operate(); 
      } finally { 
      } 
   } 
} 
  • A. beat
  • B. Not supported
  • C. The code does not compile.
  • D. The code compiles but a stack trace is printed at runtime.


C.

Note

The code does not compile due to an invalid override of the operate() method.

An overridden method must not throw any new or broader checked exceptions than the method it inherits.

Even though RuntimeException is a subclass of Exception, Exception is considered a new checked exception, since RuntimeException is an unchecked exception.

The code does not compile, and Option C is correct.




PreviousNext

Related