Skip to main content

Java Topic Practice

ArrayList Insert and Remove

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

For practice use only.

ArrayList Insert and Remove MCQ Practice

ArrayList Insert and Remove Java practice with 15 questions, code tracing, and a worked explanation for every answer.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - ArrayList Insert and Remove: track the final printed value.

import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(3);
    nums.add(7);
    nums.add(3);
    nums.add(9);
    nums.add(1, 5);
    System.out.print(nums);
  }
}

All 15 arraylist insert and remove questions

Work through the interactive quiz above first. This is the full arraylist insert and remove question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - ArrayList Insert and Remove: track the final printed value.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(3);
        nums.add(7);
        nums.add(3);
        nums.add(9);
        nums.add(1, 5);
        System.out.print(nums);
      }
    }
    • [3, 5, 7, 3, 9]
    • [3, 7, 3, 9, 5]
    • [5, 3, 7, 3, 9]
    • [3, 7, 3, 9]
    Show worked solution

    Correct answer: [3, 5, 7, 3, 9]

    Indexed add inserts at that position and shifts every later element one slot to the right.

  2. 2.Code Tracing 12 - ArrayList Insert and Remove: follow the variable updates.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(5);
        nums.add(9);
        nums.add(5);
        nums.add(11);
        int removed = nums.remove(2);
        System.out.print(removed + ":" + nums);
      }
    }
    • 11:[5, 9, 11]
    • 5:[5, 9, 11]
    • 2:[5, 9, 11]
    • 5:[5, 9, 5, 11]
    Show worked solution

    Correct answer: 5:[5, 9, 11]

    remove(int) treats its argument as an index and returns the element that was taken out.

  3. 3.Code Tracing 28 - ArrayList Insert and Remove: trace the branch and loop path.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(8);
        nums.add(8);
        nums.add(3);
        nums.add(11);
        nums.remove(0);
        nums.remove(0);
        System.out.print(nums + ":" + nums.size());
      }
    }
    • [8, 8, 3, 11]:4
    • [3, 11]:2
    • [8, 3, 11]:3
    • [8, 3, 11]:2
    Show worked solution

    Correct answer: [3, 11]:2

    Each removal shifts the remaining elements left, so removing index 0 twice drops the first two original values.

  4. 4.Code Tracing 39 - ArrayList Insert and Remove: watch the index changes.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(10);
        nums.add(10);
        nums.add(5);
        nums.add(10);
        nums.add(nums.size(), 5);
        System.out.print(nums);
      }
    }
    • [5, 10, 10, 5, 10]
    • [10, 10, 5, 10]
    • [10, 10, 5, 10, 5]
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: [10, 10, 5, 10, 5]

    Adding at exactly size() is legal and appends to the end. Only an index greater than size() throws.

  5. 5.Code Tracing 50 - ArrayList Insert and Remove: evaluate the state change step by step.

    import java.util.ArrayList;
    public class Main {
      public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(12);
        nums.add(8);
        nums.add(7);
        nums.add(9);
        nums.add(nums.size() + 1, 3);
        System.out.print(nums);
      }
    }
    • [12, 8, 7, 9, 3]
    • [12, 8, 7, 9]
    • (no output)
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: A runtime exception is thrown.

    An insert index past size() throws IndexOutOfBoundsException because it would leave a gap in the list.

  6. 6.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 1; i <= 5; i++) { l.add(i); }
    for (int i = 0; i < l.size(); i++)
    {
        if (l.get(i) % 2 == 1) { l.remove(i); }
    }
    System.out.print(l);
    • [2, 4]
    • [2, 4, 5]
    • [1, 3, 5]
    • []
    Show worked solution

    Correct answer: [2, 4]

    The forward loop skips an element after each removal, but here every odd value still happens to be reached.

  7. 7.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 1; i <= 5; i++) { l.add(i); }
    int i = 0;
    while (i < l.size())
    {
        if (l.get(i) % 2 == 1) { l.remove(i); }
        else { i++; }
    }
    System.out.print(l);
    • [2, 4, 5]
    • [2, 4]
    • [1, 3, 5]
    • []
    Show worked solution

    Correct answer: [2, 4]

    Advancing only when nothing was removed guarantees every element is examined.

  8. 8.What is printed by the following code segment?

    ArrayList<String> l = new ArrayList<String>();
    l.add("a");
    l.add("b");
    l.add(1, "c");
    l.remove("a");
    System.out.print(l);
    • [a, c]
    • [c, a]
    • [c, b]
    • [a, b]
    Show worked solution

    Correct answer: [c, b]

    The insertion places c in the middle and the by-value removal then deletes a.

  9. 9.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 0; i < 3; i++) { l.add(i, i); }
    System.out.print(l);
    • [2, 1, 0]
    • [0, 0, 0]
    • [2, 0, 1]
    • [0, 1, 2]
    Show worked solution

    Correct answer: [0, 1, 2]

    Each insertion index equals the current size, so every value is appended.

  10. 10.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 0; i < 3; i++) { l.add(0, i); }
    System.out.print(l);
    • [2, 1, 0]
    • [0, 1, 2]
    • [0, 0, 0]
    • [1, 2, 0]
    Show worked solution

    Correct answer: [2, 1, 0]

    Always inserting at the front reverses the order of insertion.

  11. 11.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 1; i <= 4; i++) { l.add(i); }
    for (int i = l.size() - 1; i >= 0; i--)
    {
        if (l.get(i) % 2 == 0) { l.remove(i); }
    }
    System.out.print(l);
    • [2, 4]
    • [1, 3]
    • [1, 2, 3]
    • []
    Show worked solution

    Correct answer: [1, 3]

    Iterating backward is immune to the shifting caused by each removal.

  12. 12.What is printed by the following code segment?

    ArrayList<String> l = new ArrayList<String>();
    l.add("a");
    l.add("b");
    l.add("c");
    l.set(1, l.remove(2));
    System.out.print(l);
    • [a, b]
    • [a, c, c]
    • [a, c]
    • [c, a]
    Show worked solution

    Correct answer: [a, c]

    The removal shrinks the list and returns c, which then replaces the element now at index 1.

  13. 13.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    for (int i = 1; i <= 3; i++) { l.add(i); }
    l.clear();
    l.add(9);
    System.out.print(l.size() + " " + l);
    • 4 [1, 2, 3, 9]
    • 0 []
    • 1 [1]
    • 1 [9]
    Show worked solution

    Correct answer: 1 [9]

    The clear method empties the list without destroying it, so later additions work normally.

  14. 14.What is printed by the following code segment?

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(1);
    l.add(2);
    l.add(3);
    int sum = 0;
    while (l.size() > 0)
    {
        sum += l.remove(0);
    }
    System.out.print(sum + " " + l.size());
    • 6 0
    • 6 3
    • 3 0
    • 0 0
    Show worked solution

    Correct answer: 6 0

    Each removal returns the element taken, and repeatedly removing the front drains the list.

  15. 15.Which statement about modifying an ArrayList during an enhanced for loop is true?

    • Changing the size during the loop is always safe.
    • Changing the size can result in a ConcurrentModificationException and should be avoided.
    • Changing the size during the loop always throws an exception.
    • The code does not compile if the list is modified inside the loop.
    Show worked solution

    Correct answer: Changing the size can result in a ConcurrentModificationException and should be avoided.

    The iterator checks for structural modification as it advances, so whether that check is reached depends on how many elements remain. A removal may throw and may equally finish without throwing, which is why an indexed loop is the right tool when a traversal changes the list.