Java OCA OCP Practice Question 1699

Question

What does the following output?

import java.util.*;

public class Main {
   public static void main(String[] args) {
      Map<Integer, Integer> map = new HashMap<>();
      map.put(9, 3);/*from  w w  w.j  a va 2s.c om*/
      Map<Integer, Integer> result = map.stream().map((k,v) -> (v,k));
      System.out.println(result.keySet().iterator().next());
   }
}
  • A. 3
  • B. 9
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

As tempting as it is, you can't actually convert a Map into a Stream directly, which means you can't call the map() method on it either.

You can build a Stream out of the keys or values or key/value pairs.

Since this code doesn't compile, Option C is correct.




PreviousNext

Related