Java OCA OCP Practice Question 291

Question

Given two files:

package rb;/* ww w  . j a  v  a 2 s . c om*/
public class Bundle extends java.util.ListResourceBundle {
  protected Object[][] getContents() {
    return new Object[][] { { "123", 456 } };
  }
}

package rb;
import java.util.*;
public class Main {
  public static void main(String[] args) {
    ResourceBundle rb = ResourceBundle.getBundle("rb.Bundle",
      Locale.getDefault());
    // insert code here
  }
}

Which, inserted independently, will compile? (Choose all that apply.)

A.   Object obj = rb.getInteger("123");
B.   Object obj = rb.getInteger(123);
C.   Object obj = rb.getObject("123");
D.   Object obj = rb.getObject(123);
E.   Object obj = rb.getString("123");
F.   Object obj = rb.getString(123);


C and E are correct.

Note

When getting a key from a resource bundle, the key must be a string.

The returned result must be a string or an object.

While that object may happen to be an integer, the API is still getObject().

E will throw a ClassCastException since 456 is not a string, but it will compile.




PreviousNext

Related