What is the result of executing the following code snippet?
List<Integer> source = new ArrayList<>(Arrays.asList(1,2,3,4)); List<Integer> fish = new CopyOnWriteArrayList<>(source); List<Integer> mammals = Collections.synchronizedList(source); Set<Integer> birds = new ConcurrentSkipListSet<>(); birds.addAll(source); // www . j a v a 2 s . co m synchronized(new Integer(10)) { for(Integer f: fish) fish.add(4); // c1 for(Integer m: mammals) mammals.add(4); // c2 for(Integer b: birds) birds.add(5); // c3 System.out.println(fish.size()+" "+mammals.size()+" "+birds.size());
F.
The code compiles without issue, so D is incorrect.
The code throws a ConcurrentModificationException at runtime on line c2, because mammals is a synchronized list and not a concurrent one.
Therefore, it is not safe to be used inside an iterator, and F is the correct answer.
Note that if line c2 were removed, the rest of the code would run without throwing an exception, outputting 8 4 5.