Java OCA OCP Practice Question 2485

Question

Given:.

4. public static void main(String[] args) {  
5.   try {  /*from w  ww  . j a v  a 2s  . c om*/
6.     if(args.length == 0)  throw new Exception();  
7.   }  
8.   catch (Exception e) {  
9.     System.out.print("done ");  
10.     doStuff();  // assume this method compiles  
11.   }  
12.   finally {  
13.     System.out.println("finally ");  
14.   }  
15. } 

Which are possible outputs? (Choose all that apply.)

  • A. "done "
  • B. "finally "
  • C. "done finally "
  • D. Compilation fails.
  • E. No output is produced.


A, B, and C are correct.

Note

Typically, if you invoked the program without a command-line argument, an exception would be thrown and the output would be "done finally".

If you invoked the program with a command-line argument, no exception would be thrown and the finally statement would produce the output: "finally".

If, however, doStuff() did something like call System.exit(1), then a no-arg invocation could produce "done".




PreviousNext

Related