Java OCA OCP Practice Question 2136

Question

What is the output of the following?.

1:   package mypkg; 
2:   import java.util.*; 
3: //from w  w w  .  java 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("mypkg.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.


B.

Note

This code compiles and runs without issue.

It creates a default Java class resource bundle.

Lines 5 through 7 show it has one key and one ArrayList value.

Line 9 gets a reference to the resource bundle.

Lines 10 through 16 retrieve the ArrayList and add six values to it.

Since this is a reference, line 17 gets the same object and prints the size of 6.

Therefore, Option B is correct.




PreviousNext

Related