Question 1
Two built-in gatherers are composed with Gatherer.andThen: the stream is first windowed, and the windows are then fed to a scan. What does this program print? ```java import java.util.List; import java.util.stream.Gatherers; import java.util.stream.Stream; public class Main { public static void main(String[] args) { List<Integer> out = Stream.of(1, 2, 3, 4, 5, 6) .gather(Gatherers.<Integer>windowFixed(2) .andThen(Gatherers.scan(() -> 0, (acc, w) -> acc + w.get(1)))) .toList(); System.out.println(out); } } ```
A. [2, 6, 12]Correct answer
windowFixed emits the three pairs, and scan adds each window's second element to a running state seeded at zero, emitting after each step: 2, then 6, then 12.
B. [0, 2, 6, 12]
Expects scan to emit its seed first; scan pushes state only after each integration, so the initial zero is never output.
C. [3, 10, 21]
Sums both elements of each window instead of only the second.
D. [2, 4, 6]
Forgets that the state accumulates across windows; each output is the running total, not the individual window's second element.
Explanation
Gatherer.andThen fuses the two gatherers so each fixed window becomes one input to the scan. scan keeps a running state from the initializer and emits the state after each element is integrated, never emitting the seed by itself. The running total therefore carries across successive windows.