Java OCA OCP Practice Question 2719

Question

Given that Integer.parseInt() throws NumberFormatException, and given:

3. public class Main {  
4.   public static void main(String[] args) {  
5.     try {  //from w  w w.  j  a v  a  2s. c om
6.       System.out.println(doStuff(args));  
7.     }  
8.     catch (Exception e) { System.out.println("exc"); }  
9.     doStuff(args);  
10.   }  
11.   static int doStuff(String[] args) {  
12.     return Integer.parseInt(args[0]);  
13.   }
14.} 

And, if the code compiles, given the invocation:

java Main x 

What is the result? (Choose all that apply.)

  • A. 0
  • B. exc
  • C. "exc" followed by an uncaught exception.
  • D. Compilation fails due to an error on line 4.
  • E. Compilation fails due to an error on line 9.
  • F. Compilation fails due to an error on line 11.
  • G. An uncaught exception is thrown with no other output.


C is correct.

Note

When "x" is the argument, a NumberFormatException is thrown at line 12.

Because this is a runtime exception, it doesn't have to be declared, and it's still propagated up to main().

The first time doStuff() is invoked, the NumberFormatException is handled in main(); the second time it is not handled.




PreviousNext

Related