Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
hard

ArrayList Modification During Iteration

Mark
< Prev
Next >

What is the primary issue that arises when the provided code is executed?

import java.util.ArrayList;
import java.util.Iterator;

public class IteratorTest {
    static ArrayList<String> list = new ArrayList<>();
    public static void main(String[] args) {
        list.add("A");
        list.add("B");
        list.add("C");
        Thread t = new Thread(() -> {
            synchronized(list) {
                Iterator<String> it = list.iterator();
                while (it.hasNext()) {
                    String s = it.next();
                    if (s.equals("B")) {
                        list.remove(s);
                    }
                }
            }
        });
        t.start();
    }
}
A

The iterator incorrectly returns null values, leading to a NullPointerException

B

Directly modifying the list during iteration causes ConcurrentModificationException even within a synchronized block

C

The use of the synchronized block results in a deadlock

D

The string comparison in the iterator leads to a race condition

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|