Java OCA OCP Practice Question 654

Question

What is the output of the following application?

package mypkg; /*www  .ja v a  2s  . c o m*/
public class Main { 
   public static void m() throws RuntimeException { 
      try { 
         throw new IllegalArgumentException(); 
      } catch (Error) { 
         System.out.print("Unable!"); 
      } 
   } 
   public static void main(String... count) throws RuntimeException { 
      m(); 
   } 
} 
  • A. Unable!
  • B. The application does not produce any output.
  • C. The application compiles but produces a stack trace at runtime.
  • D. The code does not compile.


D.

Note

The code does not compile because the catch block is missing a variable name, such as catch (Error e).

Option D is the correct answer.

If a variable name was added, the application would produce a stack trace at runtime and Option C would be the correct answer.

Because IllegalArgumentException does not inherit from Error, the catch block would be skipped and the exception sent to the main() method at runtime.

The declaration of RuntimeException by both methods is unnecessary since it is unchecked, although allowed by the compiler.




PreviousNext

Related