Java OCA OCP Practice Question 3270

Question

Consider the following program:

import java.util.*;

public class Main {
        public static void main(String []args) {
                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);//from   w ww  . j a  v  a2 s  .  c  om
                list.add(new HashMap<List<Integer>,
                        List<String>>()); // ADD_HASHMAP


                       for(Map element : list) {       // ITERATE
                               System.out.print(element + " ");
                       }
       }
}

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.

In the line marked with comment ITERATE, the Map type is not parameterized, so it will result in a warning (not a compiler error).

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