douglas576
douglas576 5d ago • 10 views

ArrayList element removal quiz for AP Computer Science A

Hey future CS rockstars! 🚀 Getting ready for the AP CSA exam? ArrayLists can be tricky, especially when it comes to removing elements. It's not always as straightforward as it seems, and understanding the nuances can make all the difference. Let's test your knowledge and solidify those concepts with a quick quiz! Good luck! 💻
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
isabel_carr Mar 16, 2026

📚 Quick Study Guide: ArrayList Element Removal

  • ✂️ `remove(int index)`: Removes the element at the specified index. All subsequent elements are shifted to the left, and their indices decrease by one. This method returns the element that was removed.
  • 🎯 `remove(Object obj)`: Removes the *first* occurrence of the specified object from the list. If the list does not contain the element, it remains unchanged. Returns `true` if the element was removed, `false` otherwise.
  • 🔄 Iteration Caution: When removing elements by index within a loop, it's generally safer and more efficient to iterate backwards from the end of the list. Iterating forwards can lead to skipping elements or `IndexOutOfBoundsException` due to element shifting.
  • ⏱️ Efficiency: Removing elements from the middle or beginning of a large `ArrayList` is an $O(N)$ operation because it requires shifting all subsequent elements. Removing from the end is $O(1)$.
  • ⚠️ Autoboxing Alert: Be extremely careful with `remove()` and `Integer` objects. If `list` contains `Integer` objects, `list.remove(5)` will attempt to remove the element at index `5`. To remove the `Integer` object with value `5`, you must cast it: `list.remove((Object) 5)`.

🧠 Practice Quiz: ArrayList Removal Skills

1. What is the primary effect on the indices of elements in an `ArrayList` when `remove(int index)` is called?

  1. Indices of all elements after the removed one increase by one.
  2. Indices of all elements after the removed one decrease by one.
  3. Indices of elements remain unchanged, but the list size decreases.
  4. The `ArrayList` is re-indexed from scratch.

2. Consider an `ArrayList names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "David"));`. What will `names` contain after `names.remove(1);`?

  1. `["Alice", "Charlie", "David"]`
  2. `["Bob", "Charlie", "David"]`
  3. `["Alice", "Bob", "David"]`
  4. `["Alice", "Charlie", "Bob", "David"]`

3. If an `ArrayList numbers` contains `[10, 20, 30, 20, 40]`, what will be the content of `numbers` after `numbers.remove(new Integer(20));`?

  1. `[10, 30, 20, 40]`
  2. `[10, 20, 30, 40]`
  3. `[10, 30, 40]`
  4. `[10, 20, 30, 20, 40]` (no change)

4. Which of the following is the *safest and most recommended* way to remove elements from an `ArrayList` while iterating through it, especially when removing based on a condition?

  1. Iterating forwards using a standard `for` loop with `list.remove(i)`.
  2. Iterating backwards using a standard `for` loop with `list.remove(i)`.
  3. Using an enhanced `for` loop and calling `list.remove()`.
  4. Using an `Iterator` and its `remove()` method.

5. An `ArrayList list = new ArrayList<>();` contains `["apple", "banana", "cherry"]`. What is returned by `list.remove("banana");`?

  1. `"banana"`
  2. `true`
  3. `false`
  4. `null`

6. Given `ArrayList data = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));`. What is the result of `data.remove(2);` followed by `data.remove((Object) 4);`?

  1. `[1, 2, 5]`
  2. `[1, 3, 5]`
  3. `[1, 2, 4]`
  4. `[1, 3, 4]`

7. What is the time complexity of removing an element from the *beginning* of an `ArrayList` in the worst-case scenario?

  1. $O(1)$
  2. $O(\log N)$
  3. $O(N)$
  4. $O(N^2)$
Click to see Answers

1. B

2. A

3. A

4. D

5. B

6. A

7. C

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀