Sample71 is run - what will be the output?
import java.util.*;
import java.util.concurrent.*;
public class Sample71
{
public static void main(String[] args)
{
List<String> names = new CopyOnWriteArrayList<>(Arrays.asList("a", "b"));
for (String s : names)
{
names.add("c");
System.out.print(s);
}
System.out.print(" " + names.size());
}
}
Answer: C
The iterator works from a snapshot taken when iteration began, so it visits only a and b however many elements are added, and no ConcurrentModificationException is possible. The two additions do take effect, so the final size is four.
Please log in or register to report this question.