Chapter TWELVE
Streams


Exam Objectives

Describe Stream interface and Stream pipeline.
Use method references with Streams.

Answers

1. The correct answer is D.
IntStream.range(1, 10) produces a stream of ints from 1 to 9. Since limit is a short-circuit operation, when there's an element that fulfills both filter conditions, the stream will terminate.
When 1 is evaluated, 1 is printed in the first filter, but since false is returned, 2 is processed. This element passes the first filter, but not the second one, so 10 is printed. When 3 is processed, 1 from the first filter is printed and then 4 is processed. This element passes both conditions so 10 is printed and the stream finishes, printing 4 at the end because of the forEach operation.


2. The correct answers are A, B, and D.
limit, peek, and skip are intermediate operations. anyMatch is a terminal operation.


3. The correct answer is C.
max is the only terminal operation. sorted, flatMap, and distinct are intermediate operations.


4. The correct answer is D.
findFirst is the only short-circuit operation. reduce is a terminal operation. parallel is an intermediate operation. findNone doesn't exist.


5. The correct answer is D.
Since count is a terminal operation, it doesn't return a reference to a stream, and it cannot be chained, so a compile-time error is generated.