Java OCA OCP Practice Question 2096

Question

What is the output of the following?.

package mypkg; //www.ja v  a2 s. c  o  m
import java.util.*; 


public class MyResource extends ListResourceBundle { 
   private int count = 0; 


   @Override 
   protected Object[][] getContents() { 
      return new Object[][] {{"count", count++}}; 
   } 
   public static void main(String[] args) { 
      ResourceBundle rb = ResourceBundle.getBundle("mypkg.MyResource"); 
      System.out.println(rb.getObject("count") + " " + rb.getObject("count")); 
   } 
} 
  • A. 0 0
  • B. 0 1
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


A.

Note

This class correctly creates a Java class resource bundle.

It extends ListResourceBundle and creates a 2D array as the property contents.

Since count is an int, it is autoboxed into an Integer.

In the main() method, it gets the resource bundle without a locale and requests the count key.

Since Integer is a Java Object, it calls getObject() to get the value.

The value is not incremented each time because the getContents() method is only called once.

Therefore, Option A is correct.




PreviousNext

Related