Courseiva
Question 94 of 513
Working with Streams and Lambda ExpressionshardMultiple ChoiceObjective-mapped

1Z0-829 Working with Streams and Lambda Expressions Practice Question

A developer implements a reduction using reduce() to concatenate strings from a stream. The code: Optional<String> result = stream.reduce((s1, s2) -> s1.concat(s2)); The operation works but the developer is concerned about performance with large streams. Which change would most likely improve performance?

⚠ Common exam trap

A common mix-up: candidates assume `reduce()` with an identity or parallel streams will fix performance, but the core issue is the immutable nature of `String` concatenation, which `collect()` with a mutable container like `StringBuffer` or `StringBuilder` resolves.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use StringBuffer with collect() instead.

`StringBuffer` is mutable and thread-safe, making it efficient for accumulating strings in a parallel or sequential stream via `collect()`. The `reduce()` operation with `concat()` creates a new `String` object for each pair, leading to O(n) string copying and quadratic time complexity. Using `collect()` with `StringBuffer` avoids this overhead by reusing a mutable buffer.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Use StringBuffer with collect() instead.

    Why this is correct

    StringBuilder (or StringBuffer) as a mutable reduction with collect() reduces intermediate allocations.

  • Use a sequential stream.

    Why it's wrong here

    Sequential vs parallel does not address the string concatenation overhead.

  • Use parallelStream with reduce.

    Why it's wrong here

    Parallel reduce may add threading overhead and still produce many intermediate strings.

  • Use reduce with identity "".

    Why it's wrong here

    Even with identity, reduce still creates many intermediate String objects.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 25, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.