1Z0-809 · Question #226
Given the code fragment: ``java List<String> nums = Arrays.asList("EE", "SE", "SE"); String ans = nums .parallelStream() .reduce("Java ", (a, b) -> a.concat(b)); System.out.print(ans); `` What is…
The correct answer is D. Java EEJava SE. Note: The list in this question appears to contain a typo - the third element "SE" is likely extraneous, since the expected result "Java EEJava SE" only maps cleanly to a two-element list ["EE", "SE"]. The explanation below treats the intended list as ["EE", "SE"]. --- Option D…
Question
List<String> nums = Arrays.asList("EE", "SE", "SE");
String ans = nums
.parallelStream()
.reduce("Java ", (a, b) -> a.concat(b));
System.out.print(ans);
What is the result?Options
- AJava EEJava EESE
- BJava EESE
- CThe program prints either: Java EEJava SE or Java SEJava EE
- DJava EEJava SE
How the community answered
(38 responses)- A8% (3)
- B3% (1)
- C16% (6)
- D74% (28)
Explanation
Note: The list in this question appears to contain a typo - the third element "SE" is likely extraneous, since the expected result "Java EEJava SE" only maps cleanly to a two-element list ["EE", "SE"]. The explanation below treats the intended list as ["EE", "SE"].
Option D is correct because parallelStream() with reduce(identity, accumulator) injects the identity value once per partition, not once globally - when ["EE", "SE"] is split into two sub-streams, each receives its own "Java " prefix, producing "Java EE" and "Java SE" independently, which are then concatenated in encounter order to yield "Java EEJava SE". Option B ("Java EESE") is the sequential result, where the identity is applied only once at the start before all elements are folded in. Option C is wrong because List-backed streams are ordered, so even parallel combination preserves encounter order - "EE" always precedes "SE". Option A doesn't correspond to any real partition of this stream. Memory tip: Think "parallel = per-partition identity" - every time the stream splits, the identity re-injects, which is exactly why the Java docs warn that your identity must be a true identity (e.g., 0 for addition, "" for concat) for parallel reduce to behave like sequential; using "Java " here violates that contract and reveals the extra prefix.
Community Discussion
No community discussion yet for this question.