Skip to content
Sharp

Collections · interview question

Given the following ArrayList operations, what will be the state of the list after execution? List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add(1, "Cherry"); list.remove("Banana");?

Multiple choiceHardchapter-4-curatedpremiumcollectionslist-interface-and-arraylist
java
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add(1, "Cherry");
list.remove("Banana");
Choose one answer
Answer and explanation

Correct answer

A. ["Apple", "Cherry"]

Why

The list initially contains "Apple" and "Banana". The operation list.add(1, "Cherry") inserts "Cherry" at index 1, resulting in ["Apple", "Cherry", "Banana"]. Then, list.remove("Banana") removes the element "Banana" by value, leaving ["Apple", "Cherry"].

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →