Question 116 of 513
1Z0-829 Index shifting during removal Practice Question
A developer writes a method that uses a for loop to iterate over a list of strings and remove elements that match a specific pattern using the list's remove(int index) method. The developer uses an index variable that increments normally. However, after running the method, some elements that should have been removed are still present, and some elements are skipped. The list initially contains [A, B, C, D, E] and the developer expects to remove B and D. After the loop, the list is [A, C, E] as expected? Actually, the developer observes that after the loop, the list contains [A, C, D, E] (D was not removed). What is the most likely cause?
⚠ Common exam trap
Oracle often tests the subtlety that removing elements from a list while iterating forward with an index variable causes elements to be skipped due to index shifting, leading candidates to mistakenly think the issue is with the remove method's index or an iterator exception.
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
✓
The for loop uses an index that becomes invalid after removal.
When using a for loop with an index variable that increments normally, removing an element via `remove(int index)` causes all subsequent elements to shift left. This means the loop effectively skips the element that moves into the current index after a removal. In this scenario, after removing B at index 1, element C moves to index 1, but the loop increments i to 2, so C is never checked. Then at i=2, the loop sees D (originally at index 3). If the removal condition for D is checked at this point and matches, D would be removed, yielding [A, C, E]. However, the observed result is [A, C, D, E], indicating D was not removed. This can happen if the loop's condition uses a fixed bound (e.g., original size) causing an `IndexOutOfBoundsException` (not shown) or if the removal pattern is based on original indices that become invalid after the first removal. The most likely cause is that the index variable becomes stale after removal, leading to skipped elements or missed removals.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The for loop uses an index that becomes invalid after removal.
Why this is correct
After removal, elements shift left, so incrementing i causes skipping of the next element.
- ✗
The list is unmodifiable, so remove throws UnsupportedOperationException.
Why it's wrong here
The list is modifiable as elements are removed.
- ✗
The remove method is called with the wrong index.
Why it's wrong here
The index is correct at the time of call, but subsequent indices shift.
- ✗
The for loop uses an iterator that throws ConcurrentModificationException.
Why it's wrong here
This is an index-based loop, not using an iterator.
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 →
Last reviewed: Jun 30, 2026
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.
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.
Sign in to join the discussion.