| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
hard
ArrayList Concurrent Modification
< Prev
Next >

Which potential concurrency issue is demonstrated by the execution of the following code snippet?

import java.util.ArrayList;

public class ExceptionThread {
    static ArrayList<Integer> list = new ArrayList<>();
    public static void main(String[] args) {
        list.add(1);
        list.add(2);
        Thread t = new Thread(() -> {
            try {
                list.remove(0);
            } catch(Exception e) {
                System.out.println("Error");
            }
        });
        t.start();
        // Main thread iterates concurrently without synchronization
        for (Integer i : list) {
            System.out.println(i);
        }
    }
}
A

Deadlock due to the exception handling block locking the entire list

B

Memory barrier violation because of the generic exception catch block

C

Race condition in the computation of element removal

D

Concurrent modification of the list during iteration that may trigger a ConcurrentModificationException

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