Java OCA OCP Practice Question 1713

Question

What is the output of this code?

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

   public static void main(String[] args) {
      Stream<Boolean> bools = Stream.iterate(true, b -> !b);
      Map<Boolean, List<Boolean>> map = bools.limit(1).collect(Collectors.partitioningBy(b -> b));
      System.out.println(map);//from  w ww . java  2 s . c  o  m

   }
}
  • A. {true=[true]}
  • B. {false=null, true=[true]}
  • C. {false=[], true=[true]}
  • D. None of the above


C.

Note

The first intermediate operation, limit(1), turns the infinite stream into a stream with one element: true.

The partitioningBy() method returns a map with two keys, true and false, regardless of whether any elements actually match.

If there are no matches, the value is an empty list, making Option C correct.




PreviousNext

Related