Java OCA OCP Practice Question 2128

Question

What is the output of the following?.

1:   package mypkg; 
2:   import java.util.*; 
3: /* ww  w  .  j  av a  2 s .  c  o  m*/
4:   public class Main extends ListResourceBundle { 
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("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

Line 10 is incorrect.

It tries to get a bundle named Main.

However, this code is in a package and named mypkg.Main.

Therefore, a MissingResourceException is thrown, and Option D is correct.




PreviousNext

Related