AP CSA Unit-Level MCQ

ArrayList Basics

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

What arraylist basics covers

An ArrayList grows and shrinks at runtime, unlike a fixed-size array. It reports its size through size(), reads with get(i), replaces with set(i, value), and appends with add(value). The type parameter must be a class type, so an ArrayList of integers is declared as ArrayList<Integer> rather than ArrayList<int>. Valid indexes still run from 0 through size() - 1.

Where students lose points

Mixing up the three accessors is constant: length for arrays, length() for Strings, size() for ArrayList. Students also index with square brackets instead of get, declare ArrayList<int>, and assume set adds a new element when it only replaces an existing one.

How it shows up on the AP exam

Unit 4 data collections. ArrayList appears in roughly every exam administration, and the API details are exactly what the multiple-choice section checks.

For practice use only.

ArrayList Basics MCQ Practice

AP CSA ArrayList basics practice: size versus length, get and set, adding elements, and the generic type rules the exam expects.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - ArrayList Basics: 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(5);
    nums.set(1, nums.get(0) + nums.get(2));
    int moved = nums.remove(3);
    nums.add(1, moved);
    int total = 0;
    for (int i = 0; i < nums.size(); i++) {
      if (i % 2 == 0) total += nums.get(i);
      else total -= nums.get(i);
    }
    System.out.print(nums + ":" + total);
  }
}

All 15 arraylist basics questions

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

  1. 1.Code Tracing 01 - ArrayList Basics: 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(5);
        nums.set(1, nums.get(0) + nums.get(2));
        int moved = nums.remove(3);
        nums.add(1, moved);
        int total = 0;
        for (int i = 0; i < nums.size(); i++) {
          if (i % 2 == 0) total += nums.get(i);
          else total -= nums.get(i);
        }
        System.out.print(nums + ":" + total);
      }
    }
    • [3, 7, 3, 9, 5]:2
    • [5, 3, 6, 9, 3]:2
    • 9:5
    • [3, 9, 6, 3, 5]:2
    Show worked solution

    Correct answer: [3, 9, 6, 3, 5]:2

    The trace combines set, remove, indexed add, and an index-parity traversal. Each list operation changes the state before the final loop runs.

  2. 2.Code Tracing 12 - ArrayList Basics: 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);
        nums.add(1, 7);
        int removed = nums.remove(3);
        System.out.print(removed + ":" + nums);
      }
    }
    • 5:[5, 7, 9, 11]
    • [5, 7, 9, 11]
    • 11:[5, 9, 5, 11]
    • 5:[11, 9, 7, 5]
    Show worked solution

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

    add(index, value) shifts elements right. remove(index) returns the removed value and shifts later elements left.

  3. 3.Code Tracing 28 - ArrayList Basics: 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.add(10);
        nums.add(12);
        int total = 0;
        for (int i = 0; i < nums.size(); i++) {
          if (nums.get(i) % 2 == 1) total += nums.get(i);
        }
        System.out.print(total);
      }
    }
    • 14
    • 22
    • 52
    • 13
    Show worked solution

    Correct answer: 14

    The loop traverses each index and accumulates only odd values.

  4. 4.Code Tracing 39 - ArrayList Basics: 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(6);
        nums.add(14);
        for (int i = nums.size() - 1; i >= 0; i--) {
          if (nums.get(i) < 8) nums.remove(i);
        }
        System.out.print(nums);
      }
    }
    • [14, 10, 10, 10]
    • [10, 10, 10, 14]
    • [10, 10, 5, 10, 6, 14]
    • [5, 6]
    Show worked solution

    Correct answer: [10, 10, 10, 14]

    Removing from the end avoids skipping elements after a removal shifts later values.

  5. 5.Code Tracing 50 - ArrayList Basics: 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(Integer.valueOf(12));
        nums.add(Integer.valueOf(8));
        nums.add(Integer.valueOf(7));
        nums.add(Integer.valueOf(9));
        nums.add(Integer.valueOf(8));
        nums.add(Integer.valueOf(16));
        Integer target = Integer.valueOf(7);
        nums.remove(target);
        nums.add(Integer.valueOf(3));
        int total = 0;
        for (Integer value : nums) {
          total += value;
        }
        System.out.print(nums.size() + ":" + total);
      }
    }
    • 6:53
    • 7:56
    • 6:56
    • 6:60
    Show worked solution

    Correct answer: 6:56

    remove(target) uses the Integer object overload, so it removes the first matching value. The enhanced for loop then unboxes each Integer during addition.

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

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(3);
    l.add(0, 5);
    l.set(1, l.get(0) + 1);
    System.out.print(l);
    • [5, 3]
    • [5, 6]
    • [3, 6]
    • [6, 5]
    Show worked solution

    Correct answer: [5, 6]

    The insertion puts 5 at the front, and set then replaces the second element with one more than the first.

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

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

    Correct answer: [a, c] 2

    The index of the target is found first and then used to remove it, leaving two elements.

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

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(1000);
    l.add(1000);
    System.out.print(l.get(0).equals(l.get(1)));
    System.out.print(" " + (l.indexOf(1000) == 0));
    • false true
    • true false
    • false false
    • true true
    Show worked solution

    Correct answer: true true

    The equals comparison inspects the values, and indexOf also uses equals, so it finds the first match.

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

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

    Correct answer: [0, 1, 3]

    Wrapping the argument removes the element whose value is 2 rather than the element at index 2.

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

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

    Correct answer: [0, 1, 3]

    An int argument selects the by-index overload, which here happens to remove the same element.

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

    ArrayList<String> l = new ArrayList<String>();
    l.add("x");
    String old = l.set(0, "y");
    System.out.print(old + l.get(0) + l.size());
    • yy1
    • xy2
    • xy1
    • xx1
    Show worked solution

    Correct answer: xy1

    The set method returns the element it replaced and leaves the size unchanged.

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

    ArrayList<Integer> a = new ArrayList<Integer>();
    a.add(1);
    ArrayList<Integer> b = new ArrayList<Integer>();
    b.add(1);
    System.out.print(a.equals(b) + " " + (a == b));
    • false false
    • true true
    • false true
    • true false
    Show worked solution

    Correct answer: true false

    A list compares equal when its elements do, but the two references still point at different objects.

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

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(5);
    l.add(7);
    int s = 0;
    for (int i = 0; i < l.size(); i++)
    {
        s += l.get(i) * i;
    }
    System.out.print(s);
    • 7
    • 12
    • 0
    • 5
    Show worked solution

    Correct answer: 7

    The first element is multiplied by 0 and the second by 1.

  14. 14.What is the result of executing the following code segment?

    ArrayList<String> l = new ArrayList<String>();
    l.add("a");
    l.add(2, "b");
    • The list becomes [a, b].
    • An IndexOutOfBoundsException is thrown.
    • The list becomes [a, null, b].
    • The code does not compile.
    Show worked solution

    Correct answer: An IndexOutOfBoundsException is thrown.

    Insertion is allowed at any index from 0 through the size, and 2 is past that with a single element.

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

    ArrayList<Integer> l = new ArrayList<Integer>();
    l.add(4);
    l.add(2);
    l.add(0, l.remove(1));
    System.out.print(l);
    • [4, 2]
    • [2, 2]
    • [2, 4]
    • [4, 4]
    Show worked solution

    Correct answer: [2, 4]

    The removal returns the element it deleted, and that value is then inserted at the front.