Java OCA OCP Practice Question 2503

Question

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()); 
 
  • A. It outputs 4 8 5.
  • B. It outputs 8 4 5.
  • C. It outputs 8 8 8.
  • D. The code does not compile.
  • E. It compiles but throws an exception at runtime on line c1.
  • F. It compiles but throws an exception at runtime on line c2.
  • G. It compiles but throws an exception at runtime on line c3.
  • H. It compiles but enters an infinite loop at runtime.


F.

Note

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.




PreviousNext

Related