Consider:
<code class="language-java">ArrayList<Integer> a = new ArrayList<>(Arrays.asList(5, 6, 5, 5, 7, 5)); for (int i = a.size() - 1; i >= 0; i--) if (a.get(i) == 5) a.remove(i); </code></pre> What is the final state of <code>a</code>?
- A
[6, 5, 7]
- Bcheck_circle
[6, 7]
- C
IndexOutOfBoundsException
- D
[5, 6, 5, 5, 7, 5]
Explanation
Iterating backwards safely removes elements because shifting only affects already-processed (higher) indices. Every 5 is removed, leaving [6, 7].