1Z0-829 Working with Streams and Lambda Expressions Practice Question
A developer writes the following code to print a list of strings in order: list.stream().map(s -> s.toUpperCase()).forEach(System.out::print). They want to parallelize the processing but must preserve the output order. Which change is correct and most appropriate?
⚠ Common exam trap
Watch out — candidates often confuse `forEach` with `forEachOrdered`, assuming that `forEach` in a parallel stream still preserves order, or they incorrectly think synchronization in `map` can fix ordering issues.
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
✓
list.parallelStream().map(s -> s.toUpperCase()).forEachOrdered(System.out::print);
`forEachOrdered` guarantees that elements are processed in encounter order even when the stream is parallelized. The `map` operation is stateless and can run in parallel, but the terminal operation must preserve order, which `forEachOrdered` does by enforcing sequential output in the stream's encounter order.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
list.parallelStream().map(s -> s.toUpperCase()).forEach(System.out::print);
Why it's wrong here
In a parallel stream, forEach does not guarantee encounter order; output order may be inconsistent.
- ✓
list.parallelStream().map(s -> s.toUpperCase()).forEachOrdered(System.out::print);
Why this is correct
forEachOrdered ensures that processing respects the encounter order, even in a parallel stream.
- ✗
list.stream().parallel().map(s -> { synchronized(System.out) { return s.toUpperCase(); } }).forEach(System.out::print);
Why it's wrong here
Synchronizing on System.out and applying inside map is unnecessary and will cause performance degradation.
- ✗
list.stream().parallel().map(s -> s.toUpperCase()).sequential().forEach(System.out::print);
Why it's wrong here
Calling sequential() after parallel() reverts to sequential processing, losing parallelism.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.