Java OCA OCP Practice Question 758


What is the output of the following?

12:  List<String> v = new ArrayList(); 
13:  v.add("A"); 
14:  v.add("B"); 
15:  v.clear(); 
16:  v.add("C"); 
17:  v.remove(1); 
18:  System.out.println(v.size()); 
  • A. 0
  • B. 1
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.



Line 12 creates an empty ArrayList.

While it isn't recommended to use generics on only the left side of the assignment operator, this is allowed.

It just gives a warning.

Lines 13 and 14 add two elements.

Line 15 resets to an empty ArrayList.

Line 16 adds an element, so now we have an ArrayList of size 1.

Line 17 attempts to remove the element at index 1.

Since Java uses zero-based indexes, there isn't an element there and the code throws an Exception.