Java OCA OCP Practice Question 2778

Question

Given:

2. public class Main {  
3.   static String a = null;  
4.   static String s = "";  
5.   public static void main(String[] args) {  
6.     try {  //from  ww w .j av  a 2  s.c  o  m
7.       a = args[0];  
8.       System.out.print(a);   
9.       s += "t1 ";  
10.     }  
11.     catch (RuntimeException re) { s += "c1 "; }  
12.     finally { s += "f1 "; }  
13.     System.out.println(" " + s);  
14.  } 
15.} 

And two command-line invocations:

java Main  
java Main x 

What is the result?

  • A. First: f1, then: x t1
  • B. First: f1, then: x t1 f1
  • C. First: c1, then: x t1
  • D. First: c1, then: x t1 f1
  • E. First: c1 f1, then: x t1
  • F. First: c1 f1, then: x t1 f1
  • G. Compilation fails.


F   is correct.

Note

The first invocation throws an exception which is caught, then finally runs.

The second invocation throws no exception, but finally still runs.




PreviousNext

Related