Java Stream reduce with Identity and Combiner

Answer Correct answer: B — The program prints "wTablewChair TableChair" due to parallel stream reduction combining partial results.

Given: What is the result? - image

  1. Compilation fails.
  2. wTablewChair TableChair Correct Answer
  3. TableChair TableChair
  4. A RuntimeException is thrown.
  5. wTableChair TableChair

Community Votes

B
70%
A
30%

70% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

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.

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.

Community Discussion (9 comments)

Tojose 👍 6
the right answer is B. wTablewChair TableChair
c6437d5 👍 5 Selected: B
B tested correct
jugue 👍 1 Selected: B
package 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
mery17 👍 1 Selected: A
The answer is A. You can't use a numeric in (1,p)
SrinivasJasti 👍 1 Selected: B
attach1 -----> identity is w accumulator --> combines two elements into one n and m combiner ---->combines results: wTablewChair. attach2 ------>no identity provided only a combiner :TableChair
Tangaizw 👍 1
The answer is A. You can't use a numeric literal for the accumulated result variable. The code won't compile.
xplorerpj 👍 2 Selected: A
Compilation error
xplorerpj 👍 2
A This code has a compilation error because the reduce method expects elements of the same type as the accumulator function's arguments.
supersquax 👍 4
B correct! verified in online compiler.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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"?

In parallel streams, the combiner merges partial results. Here, it combines "wTable" and "wChair".

Is the second reduce result deterministic?

Yes, since it uses a two-argument reduce without a combiner on a small list, the order of addition doesn't change the final string for this specific case.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide