Java OCA OCP Practice Question 1727

Question

What does the following output?

Stream<Character> chars = Stream.generate(() -> 'a');
chars.filter(c -> c < 'b')
     .sorted()
     .findFirst()
     .ifPresent(System.out::print);
  • A. a
  • B. The code runs successfully without any output.
  • C. The code enters an infinite loop.
  • D. The code compiles but throws an exception at runtime.


C.

Note

The first line generates an infinite stream.

The stream pipeline has a filter that lets all these elements through.

Since sorted() requires all the elements be available to sort, it never completes, making Option C correct.




PreviousNext

Related