Java OCA OCP Practice Question 2112

Question

What is the output of the following?.

1:   package mypkg; 
2:   import java.util.*; 
3: //from w  w  w . j av  a  2  s . c  o  m
4:   public class Main { 
5:      protected Object[][] getContents() { 
6:         return new Object[][] { { "keys", new ArrayList<String>() }}; 
7:      } 
8:      public static void main(String[] args) { 
9:         ResourceBundle rb = ResourceBundle.getBundle("keyboard.Main"); 
10:        List<String> keys = (List) rb.getObject("keys"); 
11:        keys.add("q"); 
12:        keys.add("w"); 
13:        keys.add("e"); 
14:        keys.add("r"); 
15:        keys.add("t"); 
16:        keys.add("y"); 
17:        System.out.println(((List) rb.getObject("keys")).size()); 
18:     } 
19:  } 
  • A. 0
  • B. 6
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


D.

Note

The code attempts to create a Java class resource bundle.

However, the Main class does not extend ListResourceBundle.

It compiles, but throws an exception on line 9 because no resource bundle is actually defined.

Therefore, Option D is correct.




PreviousNext

Related