Java OCA OCP Practice Question 3023

Question

Given this code segment:

List<Map<List<Integer>, List<String>>> list = new ArrayList<>(); // ADD_MAP
Map<List<Integer>, List<String>> map = new HashMap<>();
list.add(null);                                       // ADD_NULL
list.add(map);
list.add(new HashMap<List<Integer>, List<String>>()); // ADD_HASHMAP
list.forEach(e ->  System.out .print(e + " "));        // ITERATE

Which one of the following options is correct?

  • a) this program will result in a compiler error in line marked with comment ADD_MAP
  • b) this program will result in a compiler error in line marked with comment ADD_HASHMAP
  • c) this program will result in a compiler error in line marked with comment ITERATE
  • d) When run, this program will crash, throwing a NullPointerException in line marked with comment ADD_NULL
  • e) When run, this program will print the following: null {} {}


e)

Note

the lines marked with comments ADD_MAP and ADD_HASHMAP are valid uses of the diamond operator to infer type arguments.

Calling the add() method passing null does not result in a NullPointerException.

the program, when run, will successfully print the output null {} {} (null output indicates a null value was added to the list, and the {} output indicates that Map is empty).




PreviousNext

Related