Java Stream reduce with Identity and Combiner
Short answer
B The program prints "wTablewChair TableChair" due to parallel stream reduction combining partial results.
Question

Key Takeaways
The key insight is understanding that in parallel streams, the combiner function (String::concat) combines the results of different partitions, while the accumulator processes elements within a partition.
This question tests the behavior of the parallelStream reduce method when using an identity, accumulator, and combiner. The correct output is determined by how the combiner merges partial results from multiple threads.
Common mistake: Many candidates incorrectly choose compilation failure because they believe the variable names 'n' and 'm' or the numeric literal '1' cause issues, but these are valid Java identifiers and syntax.
Expert Analysis
Why the Answer Is Correct
The code uses reduce with three arguments: identity "w", accumulator (n,m) -> n+m, and combiner String::concat. In a parallel stream, elements are split into partitions. Each partition starts with the identity "w" and applies the accumulator to its elements. For the list ["Table", "Chair"], one partition might process "Table" resulting in "wTable", and another might process "Chair" resulting in "wChair". The combiner then merges these two results: "wTable" + "wChair" = "wTablewChair". The second method attach2 uses a two-argument reduce which returns an Optional<String>. Since the stream is not empty, it contains "TableChair". Thus, the output is "wTablewChair TableChair".
Why the Other Options Are Wrong
Option A is incorrect because the code compiles successfully; there are no type mismatches or syntax errors. Option D is incorrect because no exception is thrown during execution. Options C and E are incorrect because they do not reflect the actual string concatenation order produced by the parallel reduction logic.
Community Comment Notes
Several users confirmed B is correct after testing in compilers, noting the specific output "wTablewChair TableChair". Some users initially thought A was correct due to confusion about variable types or lambda syntax, but verified that the code runs fine. One user explained the role of the combiner merging partial results like "wTable" and "wChair".
Exam Strategy
When analyzing parallel stream reduce operations, always consider how the combiner function merges partial results from different threads. Remember that the identity element is used for each thread's initial state.
Frequently Asked Questions
Why does the first reduce produce "wTablewChair"?
Is the second reduce result deterministic?
Community Votes
70% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 9 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 9
-
Tojose 👍 6the right answer is B. wTablewChair TableChair
-
supersquax 👍 4B correct! verified in online compiler.
-
xplorerpj 👍 2A This code has a compilation error because the reduce method expects elements of the same type as the accumulator function's arguments.
-
jugue 👍 1 Answered Bpackage streampac; import java.util.List; public class Q38 { public String attach1 ( List<String> data) { return data.parallelStream().reduce("w", (n,m) -> n+m, String ::concat); } public String attach2 ( List<String> data) { return data.parallelStream().reduce( (l,p) -> l + p).get(); } public static void main(String[] args) { // TODO Auto-generated method stub Q38 t= new Q38(); var list = List.of("Table", "chair"); String x= t.attach1(list); String y= t.attach2(list); System.out.print(x +" " +y); } } //the output is B wTablewchair Tablechair
-
SrinivasJasti 👍 1 Answered Battach1 -----> identity is w accumulator --> combines two elements into one n and m combiner ---->combines results: wTablewChair. attach2 ------>no identity provided only a combiner :TableChair
Show 4 more comments
-
Tangaizw 👍 1The answer is A. You can't use a numeric literal for the accumulated result variable. The code won't compile.
-
mery17 👍 1 Answered AThe answer is A. You can't use a numeric in (1,p)
-
c6437d5 👍 5 Answered BB tested correct
-
xplorerpj 👍 2 Answered ACompilation error
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.