Courseiva
Controlling Program FloweasyMultiple ChoiceObjective-mapped

1Z0-829 Controlling Program Flow Practice Question

A developer needs to iterate over a List<String> and remove elements that are null. Which approach guarantees correct behavior without throwing a ConcurrentModificationException?

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 an Iterator explicitly and call its remove() method

Iterator.remove() is the safe and recommended way to remove elements during iteration, avoiding ConcurrentModificationException. Option A is incorrect because using list.remove() inside a for-each loop modifies the list directly while iterating, causing ConcurrentModificationException. Option B is incorrect because Stream.filter() does not modify the original list; it creates a new list, which may not meet the requirement to remove elements from the original. Option C is incorrect because although a decrementing for loop may work, it is error-prone and not the guaranteed approach; it is not the idiomatic Java solution.

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 a for-each loop and call list.remove() on the current element

    Why it's wrong here

    Modifying the list during for-each iteration throws ConcurrentModificationException.

  • Use a Stream's filter() method and collect the results

    Why it's wrong here

    Filtering creates a new list, which is acceptable but not the only way; it doesn't modify the original list in place.

  • Use a traditional for loop with an index that decrements

    Why it's wrong here

    While this avoids the exception, it is not the standard Java best practice for this scenario.

  • Use an Iterator explicitly and call its remove() method

    Why this is correct

    Iterator.remove() is the safe way to remove elements during iteration.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.