Java OCA OCP Practice Question 2699

Question

What is the result of the following code?

3:    Map<Integer, Integer> map = new HashMap<>(10); 
4:    for (int i = 1; i <= 10; i++) { 
5:       map.put(i, i * i); 
6:    } 
7:    System.out.println(map.get(4)); 
  • A. 16
  • B. 25
  • C. Compiler error on line 3.
  • D. Compiler error on line 5.
  • E. Compiler error on line 7.
  • F. A runtime exception is thrown.


A.

Note

Line 3 uses the diamond operator to create the map.

Lines 5 and 7 use autoboxing to convert between the int primitive and the Integer wrapper class.

The keys map to their squared value.

1 maps to 1, 2 maps to 4, 3 maps to 9, 4 maps to 16, and so on.




PreviousNext

Related