Question 241 of 509
Working with Arrays and CollectionsmediumMultiple ChoiceObjective-mapped

Quick Answer

The correct approach is to use an Iterator and its remove() method, as this is the only safe way to remove elements from an ArrayList while iterating without throwing a ConcurrentModificationException. The Iterator’s remove() method works because it updates both the iterator’s internal cursor and the collection’s modCount in a synchronized manner, keeping the iterator’s state consistent with the underlying list. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this question tests your understanding of fail-fast iterators and the concurrent modification contract—a common trap is assuming a for-each loop or index-based loop is safe, but the for-each hides an iterator that cannot be modified externally, while index-based loops cause skipped elements or IndexOutOfBoundsException due to shifting indices. Remember the mnemonic: “Iterator remove, never loop and shove”—if you need to delete while iterating, always use the iterator’s own remove method.

1Z0-829 Working with Arrays and Collections Practice Question

This 1Z0-829 practice question tests your understanding of working with arrays and collections. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. 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.

A developer needs to remove elements from an ArrayList<String> while iterating over it. Which approach is safest and avoids ConcurrentModificationException?

Question 1mediummultiple choice
Full question →

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

Iterator<String> it = list.iterator(); while (it.hasNext()) { if (it.next().equals("x")) it.remove(); }

Option C is correct because the Iterator's remove() method is the only safe way to remove elements from a collection while iterating, as it updates both the iterator's internal cursor and the collection's modCount, preventing ConcurrentModificationException. The enhanced for-each loop (options A and D) uses an implicit iterator that does not expose a remove method, so calling list.remove() directly modifies the collection without updating the iterator's state, causing the exception. Option B avoids the exception by using an index-based loop, but it is unsafe because removing an element shifts subsequent elements left, causing the loop to skip the next element and potentially miss removals or throw an IndexOutOfBoundsException.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • for (String s : list) { if (s.equals("x")) list.remove(s); break; }

    Why it's wrong here

    Still throws ConcurrentModificationException.

  • for (int i = 0; i < list.size(); i++) { if (list.get(i).equals("x")) list.remove(i); }

    Why it's wrong here

    Index shifts and may skip or miss elements.

  • Iterator<String> it = list.iterator(); while (it.hasNext()) { if (it.next().equals("x")) it.remove(); }

    Why this is correct

    Correctly uses Iterator.remove().

    Related concept

    Read the scenario before looking for a memorised answer.

  • for (String s : list) { if (s.equals("x")) list.remove(s); }

    Why it's wrong here

    Throws ConcurrentModificationException.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates often assume the index-based loop (option B) is safe because it avoids the exception, but they overlook the logical bug of skipping elements after removal, which the exam tests as a more subtle failure than the obvious ConcurrentModificationException.

Detailed technical explanation

How to think about this question

Under the hood, ArrayList maintains a modCount field that increments on structural modifications; the iterator checks this count on each next() call and throws ConcurrentModificationException if it detects a change. The Iterator.remove() method is the only way to safely remove during iteration because it calls the list's remove method internally and then resets the iterator's expectedModCount to match the current modCount. In real-world scenarios, such as filtering a list of user sessions, using an index-based loop can cause subtle bugs when multiple elements match the removal condition, while the iterator approach guarantees correct and predictable behavior.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

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

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. 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.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

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 Arrays and Collections — This question tests Working with Arrays and Collections — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Iterator<String> it = list.iterator(); while (it.hasNext()) { if (it.next().equals("x")) it.remove(); } — Option C is correct because the Iterator's remove() method is the only safe way to remove elements from a collection while iterating, as it updates both the iterator's internal cursor and the collection's modCount, preventing ConcurrentModificationException. The enhanced for-each loop (options A and D) uses an implicit iterator that does not expose a remove method, so calling list.remove() directly modifies the collection without updating the iterator's state, causing the exception. Option B avoids the exception by using an index-based loop, but it is unsafe because removing an element shifts subsequent elements left, causing the loop to skip the next element and potentially miss removals or throw an IndexOutOfBoundsException.

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

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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

Same concept, more angles

1 more ways this is tested on 1Z0-829

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer needs to iterate over an ArrayList of integers and remove all elements that are less than 10. Which approach is best to avoid ConcurrentModificationException?

easy
  • A.Use a for loop with index and list.remove(index) decrementing index.
  • B.Use an Iterator with iterator.remove().
  • C.Use a for-each loop with list.remove().
  • D.Use list.removeIf() with a lambda expression.

Why D: Option D is correct because `list.removeIf()` is a built-in method introduced in Java 8 that safely removes elements from a collection based on a predicate, handling all iteration and modification internally without throwing ConcurrentModificationException. It uses an internal iterator that properly coordinates structural modifications, making it the most concise and reliable approach for this task.

Last reviewed: Jun 11, 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.