Java OCA OCP Practice Question 684

Question

What is the output of the following?

List<String> v = new ArrayList<>(1); 
v.add("A"); 
v.add("B"); 
v.add("Art"); 
v.remove(2); 
System.out.println(v); 
  • A. [A, B]
  • B. [A, Art, B]
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


A.

Note

While the ArrayList is declared with an initial capacity of one element, it is free to expand as more elements are added.

Each of the three calls to the add() method adds an element to the end of the ArrayList.

The remove() method call deletes the element at index 2, which is Art.

Option A is correct.




PreviousNext

Related