Java OCA OCP Practice Question 2775

Question

What is the output of the following?

Stream<String> s = Stream.empty(); 
Stream<String> s2 = Stream.empty(); 
Map<Boolean, List<String>> p = s.collect( 
   Collectors.partitioningBy(b -> b.startsWith("c"))); 
Map<Boolean, List<String>> g = s2.collect( 
   Collectors.groupingBy(b -> b.startsWith("c"))); 
System.out.println(p + " " + g); 
  • A. {} {}
  • B. {} {false=[], true=[]}
  • C. {false=[], true=[]} {}
  • D. {false=[], true=[]} {false=[], true=[]}
  • E. The code does not compile.
  • F. An exception is thrown.


C.

Note

The partitioningBy() operation always returns a map with two Boolean keys, even if there are no corresponding values.

By contrast, groupingBy() returns only keys that are actually needed.




PreviousNext

Related