What is the result of compilation and running the following code?

import java.util.*;

public class Test {
    public static void main(String[] args) {
        List list = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }

        for (Object e1 : list) {
            for (Object e2 : list) {
                if (e1.equals(e2)) {
                    list.remove(e1);
                    list.remove(e2);
                }
            }
        }
        System.out.println(list.size());
    }
}
Explanation
The exception ConcurrentModificationException will occur, because there are simultaneous modifications to the list while iteration over it's elements in a loop is in progress. That is unacceptable.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes