Java OCA OCP Practice Question 2140

Question

What is the output of the following?.

package mypkg; // w ww  . ja v a  2s .c  o  m
import java.util.*; 


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


   @Override 
   protected Object[][] getContents() { 
      return new Object[][] { { "count", count++ } }; 
   } 
   public static void main(String[] args) { 
      ResourceBundle rb = new ResourceBundle("mypkg.MyResouceBundle"); 
      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.


C.

Note

ResourceBundle is an abstract class and uses a factory to obtain the right subclass.

Since a call to the constructor new ResourceBundle() does not compile, Option C is the answer.

If this was fixed, Option A would be the answer because getContents() is only called once.




PreviousNext

Related