1Z0-829 Working with Arrays and Collections Practice Question
Which of the following correctly describes the behavior of the following code? List<String> list = new ArrayList<>(); list.add("A"); list.add("B");
for (String s : list) {
if (s.equals("A")) {list.remove(s);
} }
System.out.println(list);
⚠ Common exam trap
A common mix-up: candidates think the for-each loop will safely remove the element and continue, or that the exception only occurs with multiple threads, but in fact any structural modification (add, remove, clear) to the underlying collection during single-threaded iteration triggers the fail-fast mechanism.
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
✓
ConcurrentModificationException is thrown
The code uses an enhanced for-each loop to iterate over an ArrayList while calling `list.remove(s)` when the element equals "A". This modifies the list structurally during iteration, which causes the iterator (used internally by the for-each loop) to detect the concurrent modification and throw a `ConcurrentModificationException` at runtime. The exception is thrown because the iterator's `modCount` check fails when the list is modified outside of the iterator's own `remove` method.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
ConcurrentModificationException is thrown
Why this is correct
Fail-fast behavior.
- ✗
Compilation error
Why it's wrong here
Code compiles correctly.
- ✗
[B]
Why it's wrong here
Exception thrown before completing iteration.
- ✗
[A, B] unchanged
Why it's wrong here
Modification causes exception.
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.