| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
hard Solved by 1 students
ArrayList Modification During Iteration
< 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 use of the synchronized block results in a deadlock

B

The iterator incorrectly returns null values, leading to a NullPointerException

C

The string comparison in the iterator leads to a race condition

D

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

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy