What will the following code's execution print to console?

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Test {

    private static void removeAndPrint(List<String> list) {
        for (String str : list) {
            if (str.equals("two")) {
                list.remove("three");
            }
        }
        System.out.println(list);
    }

    public static void main(String[] args) {
        List<String> list = new CopyOnWriteArrayList<String>();
        
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");

        removeAndPrint(list);

    }
}
Explanation
CopyOnWriteArrayList is a List variation that supports a remove() method call while iterating through the list without throwing a ConcurrentModificationException exception.
Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback