OCP Streams and Lambda Expressions 3 — Questions and Answers
Question 1: Which stream operation is guaranteed to be stateful and may require processing the entire stream before producing output?
- sorted() (Correct answer)
- filter()
- map()
- peek()
Correct answer: sorted()
sorted() is a stateful intermediate operation because it must examine all elements to determine their order before emitting any results.
Question 2: What is the difference between findFirst() and findAny() on a sequential stream?
- On sequential streams they behave identically, always returning the first element (Correct answer)
- findFirst() returns the last element, findAny() returns the first
- findAny() is only available on parallel streams
- findFirst() blocks until the stream is fully consumed
Correct answer: On sequential streams they behave identically, always returning the first element
On sequential streams both findFirst() and findAny() return the first element encountered; the distinction matters only for parallel streams where findAny() may return any element for performance.
Question 3: What happens when you call a terminal operation on a stream that has already been consumed?
- IllegalStateException is thrown at runtime (Correct answer)
- The stream resets and re-processes from the start
- An empty result is returned
- A new stream is automatically created
Correct answer: IllegalStateException is thrown at runtime
Streams cannot be reused; calling a second terminal operation on an already-consumed stream throws IllegalStateException.
Question 4: Which collector groups stream elements into a Map<Boolean, List<T>>?
- Collectors.partitioningBy(Predicate) (Correct answer)
- Collectors.groupingBy(Function)
- Collectors.toMap(Function, Function)
- Collectors.counting()
Correct answer: Collectors.partitioningBy(Predicate)
Collectors.partitioningBy(Predicate) divides stream elements into exactly two groups (true/false) and returns a Map<Boolean, List<T>>.
Question 5: What is the return type of Stream.collect(Collectors.toList())?
- List<T> (Correct answer)
- Collection<T>
- ArrayList<T>
- Optional<List<T>>
Correct answer: List<T>
Collectors.toList() accumulates elements into a List<T>; the specification does not guarantee ArrayList, but the return type in the API is List<T>.
Question 6: Which of the following correctly uses method references to print each element of a stream?
- stream.forEach(System.out::println) (Correct answer)
- stream.forEach(System.out.println())
- stream.map(System.out::println)
- stream.peek(System.out::println).collect()
Correct answer: stream.forEach(System.out::println)
System.out::println is an instance method reference that matches the Consumer<T> expected by forEach(), making it the correct form.
Question 7: What does the Collectors.counting() collector return?
- Long (Correct answer)
- Integer
- OptionalLong
- int
Correct answer: Long
Collectors.counting() is equivalent to reducing(0L, e -> 1L, Long::sum) and produces a Long count of the input elements.
Which stream operation is guaranteed to be stateful and may require processing the entire stream before producing output?