Java OCA OCP Practice Question 2585

Question

Given:

5.  static String s = "";  
6.  public static void main(String[] args) {  
7.    try { m(args); }  
8.    catch (Error e) { s += "e "; }  
9.    s += "x ";  
10.    System.out.println(s);  //from w  w  w . j  a  va 2s .  co m
11.  }   
12.  static void m(String[] args) {  
13.    if(args.length == 0)  
14.      throw new IllegalArgumentException();  
15.    s += "d ";  
16.  } 

And, if the code compiles, and given a java invocation with no arguments, what is the result? (Choose all that apply.).

  • A. d x
  • B. e x
  • C. d e x
  • D. Compilation fails due to an error on line 8.
  • E. Compilation fails due to an error on line 12.
  • F. Compilation fails due to an error on line 14.
  • G. An uncaught IllegalArgumentException is thrown.


G is correct.

Note

It's legal, although uncommon to catch Errors.

It's legal to shadow the variable name "args".

It's legal to throw a runtime exception without handling it or declaring it.

And finally, the catch block doesn't catch the exception because exceptions don't extend from Error.




PreviousNext

Related