ArrayList list = new ArrayList(); list.add("Beijing"); list.add("Tokyo"); list.add("Shanghai"); list.set(3, "Hong Kong");

Analyze the following code:


ArrayList list = new ArrayList();
list.add("Beijing");
list.add("Tokyo");
list.add("Shanghai");
list.set(3, "Hong Kong");



A. The last line in the code causes a runtime error because there is no element at index 3 in the array list.
B. The last line in the code has a compile error because there is no element at index 3 in the array list.
C. If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
D. If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.

The correct answer is AC

Explanation: There is no element at index 3.


Java

Learn More Multiple Choice Question :