Java OCA OCP Practice Question 2190

Question

What is the output of the following?.

package mypkg; //  w  ww.j ava2  s  .com
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.getString("count") + " " + rb.getString("count")); 
   } 
} 
  • A. 0 0
  • B. 0 1
  • C. 1 1
  • D. 1 2
  • E. The code does not compile.
  • F. The code compiles but throws an exception at runtime.


F.

Note

This class correctly creates and retrieves a Java class resource bundle.

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

However, rb.getString() cannot be called for an Integer value.

The code throws a ClassCastException, so Option F is the answer.

If this was fixed, the answer would be Option C because the pre-increment is used and getContents() is only called once.




PreviousNext

Related