Java OCA OCP Practice Question 1187

Question

Given:

12. public class MyClass {
13.   private Map map = new HashMap();
14.   private int retirementFund;
15.//from   w  w w . j av  a  2s.co  m
16.   public int getBalance(String n) {
17.     Integer total = (Integer) map.get(n);
18.     if (total == null)
19.       total = Integer.valueOf(0);
20.     return total.intValue();
21.   }
23.   public void setBalance(String n, int amount) {
24.     map.put(n, Integer.valueOf(amount));
25.   }
26. }

This class is to be updated to make use of appropriate generic types, with no changes in behavior (for better or worse).

Which of these steps could be performed? (Choose three.)

A.   Replace line 13 with//  w  w  w  . j a  v  a  2 s. c om

     private Map<String, int> map = new HashMap<String, int>();

B.   Replace line 13 with

     private Map<String, Integer> map = new HashMap<String, Integer>();

C.   Replace line 13 with

     private Map<String<Integer>\> map = new HashMap<String<Integer>\>();

D.   Replace lines 17-20 with

     int total = map.get(n);
         if (total == null)
             total = 0;
         return total;

E.   Replace lines 17-20 with

     Integer total = map.get(n);
         if (total == null)
             total = 0;
         return total;

F.  Replace lines 17-20 with

      return map.get(n);

G.  Replace line 24 with

      map.put(n, amount);

H.  Replace line 24 with

      map.put(n, amount.intValue());


B, E, and G are correct.

Note

A is incorrect because you can't use a primitive type as a type parameter.

C is incorrect because a Map takes two type parameters separated by a comma.

D is incorrect because an int can't autobox to a null, and F is incorrect because a null can't unbox to 0.

H is incorrect because you can't autobox a primitive just by trying to invoke a method with it.




PreviousNext

Related