Java OCA OCP Practice Question 662

Question

What is the output of the following application?

package mypkg; //  w w  w  . ja v  a  2 s. c om
interface Printable { 
   void print() throws Exception; 
} 
public class Main implements Printable { 
   public void print() { 
      try { 
         throws new RuntimeException("Circuit Break!"); 
      } finally { 
         System.out.print("Flipped!"); 
      } 
   } 
   public static void main(String... electricity) throws Throwable { 
      final Printable bulb = new Main(); 
      bulb.print(); 
   } 
} 
  • A. A stack trace for a RuntimeException
  • B. Flipped!, followed by a stack trace for a RuntimeException
  • C. The code does not compile because print() is an invalid method override.
  • D. The code does not compile for another reason.


D.

Note

The question is designed to see how closely you pay attention to throw and throws!

The try block uses the incorrect keyword, throws, to create an exception.

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

If throws was changed to throw, then the code would compile without issue, and Option B would be correct.




PreviousNext

Related