1Z0-809 · Question #59
Which statement is true about java.util.Stream?
The correct answer is A. A stream cannot be consumed more than once. Once a Stream is consumed by a terminal operation (like collect(), forEach(), or count()), it is closed and cannot be reused - attempting to do so throws an IllegalStateException, making A correct. B is false because a stream's execution mode (sequential vs. parallel) is set at…
Question
Options
- AA stream cannot be consumed more than once.
- BThe execution mode of streams can be changed during processing.
- CStreams are intended to modify the source data.
- DA parallel stream is always faster than an equivalent sequential stream.
How the community answered
(59 responses)- A76% (45)
- B3% (2)
- C5% (3)
- D15% (9)
Explanation
Once a Stream is consumed by a terminal operation (like collect(), forEach(), or count()), it is closed and cannot be reused - attempting to do so throws an IllegalStateException, making A correct. B is false because a stream's execution mode (sequential vs. parallel) is set at creation or via parallel()/sequential() calls, but once a terminal operation begins, the mode is fixed - you can't switch mid-processing. C is false because streams are designed for read-only functional-style processing; modifying the source during stream traversal leads to undefined behavior and is explicitly discouraged. D is false because parallel streams introduce thread-coordination overhead, so for small datasets or simple operations, sequential streams are often faster - parallelism helps only when the workload is large enough to offset that cost.
Memory tip: Think of a stream like a garden hose - once the water (data) flows through and the tap (terminal operation) is turned off, you need a new hose for the next run. "One flow, one go."
Community Discussion
No community discussion yet for this question.