Question 48 of 519
Working with Streams and Lambda ExpressionseasyMultiple ChoiceObjective-mapped

1Z0-829 Stream.iterate Practice Question

This 1Z0-829 practice question tests your understanding of working with streams and lambda expressions. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. A key principle to apply: stream.iterate. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

Exhibit

Refer to the exhibit.
List<Integer> nums = List.of(1, 2, 3, 4, 5);
Optional<Integer> opt = nums.stream().filter(n -> n % 2 == 0).findFirst();
System.out.println(opt.orElse(-1));

What is the output?

Exhibit

Refer to the exhibit.
List<Integer> nums = List.of(1, 2, 3, 4, 5);
Optional<Integer> opt = nums.stream().filter(n -> n % 2 == 0).findFirst();
System.out.println(opt.orElse(-1));

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

2

The code uses `Stream.iterate(0, n -> n - 1)` to generate an infinite stream starting from 0, decrementing by 1 each step. `limit(5)` restricts to the first five elements: 0, -1, -2, -3, -4. The `filter(n -> n % 2 != 0)` retains only odd numbers: -1 and -3. Then `map(n -> n * 2)` doubles them to -2 and -6. Finally, `reduce(0, Integer::sum)` sums these with identity 0, yielding -8. Since -8 is not among the provided options (1, 2, 0, -1), none of the options is correct.

Key principle: Stream.iterate

Answer analysis

Option-by-option breakdown

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

  • 1

    Why it's wrong here

    Option A is 1, which would result from a different stream processing (e.g., if limit were 1 and map doubled 0.5, but here the output is -8).

  • 2

    Why this is correct

    Option B is 2, which would result from the commonly assumed iterate(n -> n+1) with limit(2), but the actual code uses decrement, yielding -8.

    Related concept

    Stream.iterate

  • 0

    Why it's wrong here

    Option C is 0, which would occur if the filter removed all elements or if the identity were the only value, but here we have -1 and -3.

  • -1

    Why it's wrong here

    Option D is -1, which is close but incorrect; the sum of doubled odd numbers from the first five decremented integers is -8.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Candidates often overlook that the iterate function can be subtraction, leading to negative numbers. They may misapply the limit or filter, or forget that reduce uses an identity value. The key is to trace each step carefully and realize that -8 is the actual output, which is not listed.

Trap categories for this question

  • Command / output trap

    Option A is 1, which would result from a different stream processing (e.g., if limit were 1 and map doubled 0.5, but here the output is -8).

Detailed technical explanation

How to think about this question

The `reduce` operation with identity `0` and accumulator `(a, b) -> a - b` performs a left-associative subtraction: for elements [2, 6], it computes ((0 - 2) - 6) = -8. This differs from `Integer::sum` which would add. Understanding the associativity and identity of reduction is crucial for correct stream processing, especially in parallel streams where order matters. In real-world scenarios, using the wrong accumulator can lead to subtle bugs in aggregations like financial calculations.

KKey Concepts to Remember

  • Stream.iterate
  • limit
  • filter
  • reduce

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Stream.iterate

Real-world example

How this comes up in practice

A practitioner preparing for the 1Z0-829 exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. Stream.iterate Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

What to study next

Got this wrong? Here's your next step.

Review stream.iterate, then practise related 1Z0-829 questions on the same topic to reinforce the concept.

Related practice questions

Related 1Z0-829 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 1Z0-829 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 1Z0-829 question test?

Working with Streams and Lambda Expressions — This question tests Working with Streams and Lambda Expressions — Stream.iterate.

What is the correct answer to this question?

The correct answer is: 2 — The code uses `Stream.iterate(0, n -> n - 1)` to generate an infinite stream starting from 0, decrementing by 1 each step. `limit(5)` restricts to the first five elements: 0, -1, -2, -3, -4. The `filter(n -> n % 2 != 0)` retains only odd numbers: -1 and -3. Then `map(n -> n * 2)` doubles them to -2 and -6. Finally, `reduce(0, Integer::sum)` sums these with identity 0, yielding -8. Since -8 is not among the provided options (1, 2, 0, -1), none of the options is correct.

What should I do if I get this 1Z0-829 question wrong?

Review stream.iterate, then practise related 1Z0-829 questions on the same topic to reinforce the concept.

What is the key concept behind this question?

Stream.iterate

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

Keep practising

More 1Z0-829 practice questions

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.