Java OCA OCP Practice Question 2281

Question

Choose the correct option based on this program:

import java.util.concurrent.*;
import java.util.*;

public class Main {
    public static void main(String []args) {
        ArrayList<Integer> aList =
                        new CopyOnWriteArrayList<Integer>(); // LINE A
        aList.addAll(Arrays.asList(10, 20, 30, 40));
        System.out.println(aList);
    }/*from  w  w w.j a va2s.c om*/
}
  • A. When executed the program prints the following: [10, 20, 30, 40].
  • B. When executed the program prints the following: CopyOnWriteArrayList.class.
  • C. the program does not compile and results in a compiler error in line marked with comment LINE A.
  • D. When executed the program throws a runtime exception ConcurrentModificationException.
  • e. When executed the program throws a runtime exception InvalidOperationException.


C.

Note

the class CopyOnWriteArrayList does not inherit from ArrayList, so an attempt to assign a CopyOnWriteArrayList to an ArrayList reference will result in a compiler error.

note that the ArrayList suffix in the class named CopyOnWriteArrayList could be misleading as these two classes do not share an IS-a relationship.




PreviousNext

Related