Java OCA OCP Practice Question 2840

Question

What is the result of executing the following code snippet?

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;

public class Main {

   public static void main(String[] args) {
      List<Integer> l1 = Arrays.asList(1, 2, 3);
      List<Integer> l2 = new CopyOnWriteArrayList<>(l1);
      Set<Integer> s3 = new ConcurrentSkipListSet<>();
      s3.addAll(l1);//from   www .  j a  va 2 s . c  o  m

      for (Integer item : l2)
         l2.add(4); // x1
      for (Integer item : s3)
         s3.add(5); // x2
      System.out.println(l1.size() + " " + l2.size() + " " + s3.size());

   }
}
  • A. It outputs 3 6 4.
  • B. It outputs 6 6 6.
  • C. It outputs 6 3 4.
  • D. The code does not compile.
  • E. It compiles but throws an exception at runtime on line x1.
  • F. It compiles but throws an exception at runtime on line x2.
  • G. It compiles but enters an infinite loop at runtime.


A.

Note

The code compiles without issue, so D is incorrect.

The CopyOnWriteArrrayList class is designed to preserve the original list on iteration, so the first loop will be executed exactly three times and E is incorrect.

The ConcurrentSkipListSet class allows modifications while iterating, so it is possible that the second loop could generate an infinite loop.

In this case, though, the second loop executes exactly four times, since elements in a set are unique and 5 can be added only once.

For these reasons, F and G are also incorrect.

Finally, despite using the elements of l1 to populate the collections, l2 and s3 are not backed by the original list, so the size of l1 is 3.

Likewise, the size of l2 is 6 and the size of s3 is 4, so A is the correct answer.




PreviousNext

Related