AP Computer Science a Quiz: Arraylist Methods
20 questions · exam conditions
0:00
Arraylist MethodsQuestion 1 of 20

Identify the error in this student database code and choose the best correction.

import java.util.ArrayList;

public class StudentDemo {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<String>();
        students.add("Mia");
        students.add("Noah");

        students.remove("0"); // intended to remove first student
        System.out.println(students.size());
    }
}
Use students.remove(0);
Use students.remove(1);
Use students.delete(0);
Use students.remove();
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Arraylist Methods

Practice Arraylist Methods in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Arraylist Methods, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

Identify the error in this student database code and choose the best correction.

import java.util.ArrayList;

public class StudentDemo {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<String>();
        students.add("Mia");
        students.add("Noah");

        students.remove("0"); // intended to remove first student
        System.out.println(students.size());
    }
}
  1. Use students.remove(0); (correct answer)
  2. Use students.remove(1);
  3. Use students.delete(0);
  4. Use students.remove();

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates an error where remove("0") is called with a string parameter instead of an integer index. Choice A is correct because remove(0) properly removes the element at index 0, which is the intended behavior. Choice B is incorrect because it would remove the second student, not the first as intended. To help students, emphasize the difference between remove(int index) and remove(Object o) methods. Practice identifying method parameter types and understanding how Java determines which overloaded method to call.

Question 2

Which method would you use to replace a book title at a specific index in an ArrayList?

import java.util.ArrayList;

public class UpdateTitle {
    public static void main(String[] args) {
        ArrayList<String> books = new ArrayList<String>();
        books.add("OldTitle");
        // replace index 0 with "NewTitle"
    }
}
  1. set(index, value) (correct answer)
  2. get(index, value)
  3. add(value, index)
  4. size(index, value)

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with specific methods for different operations. The given scenario requires replacing an existing element at a specific index with a new value. Choice A is correct because set(index, value) is the ArrayList method specifically designed to replace an element at a given index with a new value. Choice B is incorrect because get() is for retrieving values, not replacing them, and it doesn't take a value parameter. To help students, emphasize memorizing the correct method signatures and their purposes: get() retrieves, set() replaces, add() inserts. Create flashcards or reference sheets showing each method's syntax and use case.

Question 3

In this student database, what is printed after set changes one record?

import java.util.ArrayList;

public class Roster {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<String>();
        students.add("Ava");
        students.add("Ben");
        students.add("Cal");
        students.set(1, "Bea"); // replace "Ben" at index 1
        System.out.println(students.get(1));
    }
}
  1. Ben
  2. Bea (correct answer)
  3. Cal
  4. Ava

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, set, and get providing essential functionality. The given code snippet demonstrates the use of the set method to replace an element at a specific index in an ArrayList of student names. Choice B is correct because the set(1, "Bea") method replaces the element at index 1 ("Ben") with "Bea", and get(1) retrieves this new value. Choice A is incorrect due to misunderstanding that set returns the old value rather than modifying the ArrayList in place. To help students, emphasize the difference between set (which replaces) and add (which inserts). Practice using set to update existing elements and understanding that it modifies the ArrayList directly.

Question 4

In this library catalog, what is printed after inserting at index 0?

import java.util.ArrayList;

public class Catalog {
    public static void main(String[] args) {
        ArrayList<String> books = new ArrayList<String>();
        books.add("Emma");
        books.add("It");
        books.add(0, "Sula"); // insert at front
        System.out.println(books.get(1));
    }
}
  1. Sula
  2. Emma (correct answer)
  3. It
  4. 1

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add with index parameter providing essential functionality for inserting elements at specific positions. The given code snippet demonstrates inserting "Sula" at index 0, which pushes existing elements to the right. Choice B is correct because after inserting "Sula" at index 0, the ArrayList becomes ["Sula", "Emma", "It"], making "Emma" at index 1. Choice A is incorrect due to misunderstanding that the inserted element would be at index 1 rather than index 0. To help students, emphasize that add(index, element) inserts at the specified position and shifts existing elements right. Practice tracing insertions at different positions to build intuition about element shifting.

Question 5

In this library catalog, what is the output after calling size()?

import java.util.ArrayList;

public class Library {
    public static void main(String[] args) {
        ArrayList<String> books = new ArrayList<String>();
        books.add("Dune");
        books.add("1984");
        books.add("Hamlet");
        books.remove(1); // removes "1984"
        System.out.println(books.size());
    }
}
  1. 2 (correct answer)
  2. 3
  3. 1
  4. 0

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and size providing essential functionality. The given code snippet demonstrates adding three books to an ArrayList, removing one at index 1, then calling the size() method. Choice A is correct because after adding three elements ("Dune", "1984", "Hamlet") and removing the element at index 1 ("1984"), the ArrayList contains two elements, so size() returns 2. Choice B is incorrect due to the common misconception that size() returns the original size before removal. To help students, emphasize that size() returns the current number of elements in the ArrayList. Practice tracing code execution and keeping track of how many elements remain after each operation.

Question 6

In this inventory system, which line correctly updates the quantity of "Pencils" to 20?

import java.util.ArrayList;

public class InventoryDemo {
    public static void main(String[] args) {
        ArrayList<Integer> qty = new ArrayList<Integer>();
        qty.add(12); // Pencils
        qty.add(5);  // Notebooks
        qty.add(0);  // Erasers
        // update Pencils quantity here
    }
}
  1. qty.set(0, 20); (correct answer)
  2. qty.get(0) = 20;
  3. qty.add(0, 20);
  4. qty.remove(0, 20);

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates updating an existing element in an ArrayList containing inventory quantities. Choice A is correct because the set(0, 20) method correctly replaces the value at index 0 (Pencils quantity) with 20. Choice B is incorrect because get() returns a value and cannot be used for assignment, while Choice C would insert 20 at index 0 and shift existing elements right rather than replacing. To help students, emphasize the difference between set (replace) and add (insert). Practice identifying when to use each method based on whether you want to modify existing data or add new data.

Question 7

What is the output of this student database code after set and size?

import java.util.ArrayList;

public class RosterDemo {
    public static void main(String[] args) {
        ArrayList<String> roster = new ArrayList<String>();
        roster.add("Ana");
        roster.add("Bo");
        roster.add("Cal");

        roster.set(2, "Cam"); // update last name
        System.out.println(roster.size());
    }
}
  1. 2
  2. 3 (correct answer)
  3. 4
  4. 0

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the set method on an ArrayList initialized with three student names, followed by the size method. Choice B is correct because set(2, "Cam") only replaces the existing element at index 2 without changing the ArrayList's size, so size() still returns 3. Choice A is incorrect due to a common misconception that set might remove an element and reduce the size. To help students, emphasize that set replaces elements in-place without affecting the list size, unlike remove which decreases size. Practice distinguishing between operations that change size versus those that only modify content.

Question 8

In this game inventory, what is the value of item after set and get?

import java.util.ArrayList;

public class GameDemo {
    public static void main(String[] args) {
        ArrayList<String> inventory = new ArrayList<String>();
        inventory.add("Potion");
        inventory.add("Sword");
        inventory.add("Shield");
        inventory.set(0, "Elixir");
        String item = inventory.get(0);
        System.out.println(item);
    }
}
  1. Potion
  2. Sword
  3. Shield
  4. Elixir (correct answer)

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the set method to replace an element and then retrieve it with get. Choice D is correct because set(0, "Elixir") replaces "Potion" at index 0 with "Elixir", and get(0) then retrieves "Elixir". Choice A is incorrect due to a common misconception that set doesn't actually change the ArrayList contents. To help students, emphasize that set replaces the element at the specified index and returns the old value. Practice tracing through code that combines multiple ArrayList operations to understand their cumulative effects.

Question 9

What is the output of this inventory code after removing the sold-out product?

import java.util.ArrayList;

public class RemoveSoldOut {
    public static void main(String[] args) {
        ArrayList<String> products = new ArrayList<String>();
        products.add("Apples");
        products.add("Bananas");
        products.add("Carrots");
        products.remove(2);
        System.out.println(products.size() + " " + products.get(1));
    }
}
  1. 2 Bananas (correct answer)
  2. 3 Bananas
  3. 2 Carrots
  4. 3 Carrots

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates combining remove, size, and get methods to produce output. Choice A is correct because after removing index 2 ("Carrots"), the size becomes 2, and get(1) returns "Bananas", resulting in "2 Bananas" being printed. Choice B is incorrect due to a common misconception about how size changes after removal. To help students, emphasize tracing through each operation sequentially and understanding that remove both reduces size and shifts elements. Practice combining multiple ArrayList operations and predicting their cumulative effects on size and element positions.

Question 10

In this library catalog, what is printed after calling remove and then size?

import java.util.ArrayList;

public class LibraryDemo {
    public static void main(String[] args) {
        ArrayList<String> books = new ArrayList<String>(); // create catalog
        books.add("Dune");
        books.add("1984");
        books.add("Hamlet");

        books.remove(1); // remove "1984"
        System.out.println(books.size()); // number of books left
    }
}
  1. 2 (correct answer)
  2. 3
  3. 1
  4. 0

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the remove method on an ArrayList initialized with three book titles, followed by the size method. Choice A is correct because the remove(1) method removes the element at index 1 ("1984"), leaving two elements in the list, so size() returns 2. Choice B is incorrect due to a common misconception that remove doesn't actually reduce the size of the ArrayList. To help students, emphasize the importance of understanding that remove shifts subsequent elements and reduces the list size. Practice tracing code execution for ArrayList operations and identifying how indices change after removals.

Question 11

In this inventory system, what is the output after using set and then get?

import java.util.ArrayList;

public class InventoryDemo {
    public static void main(String[] args) {
        ArrayList<Integer> qty = new ArrayList<Integer>(); // quantities per item
        qty.add(5);
        qty.add(2);
        qty.add(9);

        qty.set(1, 7); // update second item's quantity
        System.out.println(qty.get(1)); // print updated quantity
    }
}
  1. 2
  2. 7 (correct answer)
  3. 9
  4. 5

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the set method on an ArrayList initialized with three integer quantities, followed by the get method. Choice B is correct because the set(1, 7) method replaces the element at index 1 with the value 7, and get(1) retrieves this updated value. Choice C is incorrect due to a common misconception that set might add to the existing value rather than replace it. To help students, emphasize the difference between set (which replaces) and add (which inserts). Practice visualizing ArrayList state changes after each method call to build intuition.

Question 12

In this social media app, what is printed after add at index 1 and then get?

import java.util.ArrayList;

public class FriendsDemo {
    public static void main(String[] args) {
        ArrayList<String> friends = new ArrayList<String>();
        friends.add("Ava");
        friends.add("Ben");
        friends.add("Cy");

        friends.add(1, "Dee"); // insert at index 1
        System.out.println(friends.get(2)); // retrieve index 2
    }
}
  1. Dee
  2. Ben (correct answer)
  3. Cy
  4. Ava

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the add method with an index parameter on an ArrayList initialized with three friend names. Choice B is correct because add(1, "Dee") inserts "Dee" at index 1, shifting "Ben" to index 2 and "Cy" to index 3, so get(2) returns "Ben". Choice A is incorrect due to a common misconception that the inserted element would be at the requested index in get(). To help students, emphasize that add with an index shifts existing elements to the right. Use visual diagrams showing the ArrayList before and after insertion to clarify index changes.

Question 13

In this game inventory, what is the output after remove and then get?

import java.util.ArrayList;

public class GameDemo {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<String>();
        items.add("Potion");
        items.add("Key");
        items.add("Map");

        items.remove(0); // use potion
        System.out.println(items.get(0)); // new first item
    }
}
  1. Potion
  2. Key (correct answer)
  3. Map
  4. null

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the remove method on an ArrayList initialized with three game items, followed by the get method. Choice B is correct because remove(0) removes "Potion" at index 0, causing "Key" to shift to index 0 and "Map" to shift to index 1, so get(0) returns "Key". Choice A is incorrect due to a common misconception that the removed element might still be accessible. To help students, emphasize that removing an element causes all subsequent elements to shift left by one position. Use before-and-after diagrams to visualize index changes.

Question 14

How would you modify the library catalog so get(1) returns "Emma" after the updates?

import java.util.ArrayList;

public class CatalogDemo {
    public static void main(String[] args) {
        ArrayList<String> authors = new ArrayList<String>();
        authors.add("Orwell");
        authors.add("Austen");
        authors.add("Tolkien");

        // Desired: authors becomes ["Orwell", "Emma", "Tolkien"]
        // Choose one line to replace Austen with Emma.
    }
}
  1. authors.set(1, "Emma"); (correct answer)
  2. authors.add(1, "Emma");
  3. authors.get(1, "Emma");
  4. authors.remove("Emma");

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code requires replacing "Austen" at index 1 with "Emma" to achieve the desired result. Choice A is correct because set(1, "Emma") replaces the element at index 1 with "Emma", achieving the desired ArrayList state. Choice B is incorrect because add(1, "Emma") would insert "Emma" at index 1, shifting "Austen" to index 2 rather than replacing it. To help students, emphasize the key difference between set (replaces existing element) and add with index (inserts and shifts). Practice choosing the appropriate method based on whether you want to replace or insert.

Question 15

In this inventory system, what is printed after size and indexed get?

import java.util.ArrayList;

public class StoreDemo {
    public static void main(String[] args) {
        ArrayList<String> products = new ArrayList<String>();
        products.add("Milk");
        products.add("Bread");
        products.add("Eggs");

        int lastIndex = products.size() - 1; // index of last item
        System.out.println(products.get(lastIndex));
    }
}
  1. Milk
  2. Bread
  3. Eggs (correct answer)
  4. 3

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the size method to calculate the last valid index, then uses get to retrieve that element. Choice C is correct because size() returns 3, making lastIndex equal to 2, and get(2) returns "Eggs", the element at index 2. Choice D is incorrect because it confuses the size of the list with the element at the last index. To help students, emphasize that ArrayList indices are zero-based, so the last index is always size() - 1. Practice calculating valid index ranges and avoiding off-by-one errors.

Question 16

Explain the result of calling remove with an int on this friends list.

import java.util.ArrayList;

public class RemoveDemo {
    public static void main(String[] args) {
        ArrayList<String> friends = new ArrayList<String>();
        friends.add("Lia");
        friends.add("Max");
        friends.add("Nia");

        friends.remove(1); // remove by index
        System.out.println(friends.get(1));
    }
}
  1. Lia
  2. Max
  3. Nia (correct answer)
  4. 1

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the remove method with an integer parameter on an ArrayList containing friend names. Choice C is correct because remove(1) removes "Max" at index 1, causing "Nia" to shift from index 2 to index 1, so get(1) returns "Nia". Choice B is incorrect due to a common misconception that the removed element might still be accessible at its original position. To help students, emphasize that remove(int) removes by index and shifts subsequent elements left. Practice tracing through removals and understanding the resulting index changes.

Question 17

Identify the error in this game code and suggest a correction.

import java.util.ArrayList;

public class ActiveEffectsDemo {
    public static void main(String[] args) {
        ArrayList<String> effects = new ArrayList<String>();
        effects.add("Shield");
        effects.add("Speed");

        effects.set(2, "Strength"); // intended to update "Speed"
        System.out.println(effects.get(1));
    }
}
  1. Change to effects.set(1, "Strength"); (correct answer)
  2. Change to effects.add(2, "Strength");
  3. Change to effects.set("Speed", "Strength");
  4. Change to effects.get(2, "Strength");

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code contains an error where set(2, "Strength") attempts to access index 2, but the ArrayList only has elements at indices 0 and 1, causing an IndexOutOfBoundsException. Choice A is correct because set(1, "Strength") properly updates the element at index 1 ("Speed") to "Strength". Choice B is incorrect because while add(2, "Strength") would work, it would add a new element rather than update the existing "Speed" element as intended. To help students, emphasize checking ArrayList bounds before accessing indices. Practice identifying valid index ranges based on the current size of the list.

Question 18

Explain the result of calling get(2) in this friends list after one removal.

import java.util.ArrayList;

public class FriendsDemo {
    public static void main(String[] args) {
        ArrayList<String> friends = new ArrayList<String>();
        friends.add("Kai");
        friends.add("Liv");
        friends.add("Mia");
        friends.add("Noah");
        friends.remove(1); // removes "Liv"
        System.out.println(friends.get(2));
    }
}
  1. Liv
  2. Mia
  3. Noah (correct answer)
  4. IndexOutOfBoundsException

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates how remove affects subsequent indices when elements shift left. Choice C is correct because after removing index 1 ("Liv"), the remaining elements shift left: "Mia" moves to index 1 and "Noah" moves to index 2, so get(2) returns "Noah". Choice B is incorrect due to a common misconception that indices don't shift after removal. To help students, emphasize visualizing the ArrayList after each operation and understanding that remove causes all elements after the removed index to shift left by one position. Practice tracing through removals and subsequent accesses.

Question 19

In this library catalog code, what is printed after remove and set are executed?

import java.util.ArrayList;

public class LibraryDemo {
    public static void main(String[] args) {
        ArrayList<String> books = new ArrayList<String>();
        books.add("Dune");          // index 0
        books.add("1984");          // index 1
        books.add("Hamlet");        // index 2
        books.remove(1);             // removes "1984"
        books.set(1, "Macbeth");    // replaces "Hamlet"
        System.out.println(books.get(1) + " " + books.size());
    }
}
  1. Hamlet 2
  2. Macbeth 2 (correct answer)
  3. Macbeth 3
  4. 1984 2

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the remove and set methods on an ArrayList initialized with three book titles. Choice B is correct because after removing index 1 ("1984"), the ArrayList shifts elements left, making "Hamlet" move to index 1, then set(1, "Macbeth") replaces "Hamlet" with "Macbeth", resulting in "Macbeth 2" being printed. Choice C is incorrect due to a common misconception that the size remains 3 after removal, when it actually becomes 2. To help students, emphasize the importance of understanding how remove shifts elements and reduces size. Practice tracing code execution step-by-step and visualizing how ArrayList indices change after each operation.

Question 20

In this student database, what does the call to remove return?

import java.util.ArrayList;

public class StudentDemo {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<String>();
        students.add("Ava");
        students.add("Ben");
        students.add("Cara");
        String removed = students.remove(1);
        System.out.println(removed);
    }
}
  1. Ava
  2. Ben (correct answer)
  3. Cara
  4. 1

Explanation: This question assesses understanding of Java ArrayList methods and their correct application. ArrayLists in Java are dynamic arrays that allow for flexible manipulation and storage of data, with methods like add, remove, and get providing essential functionality. The given code snippet demonstrates the use of the remove method on an ArrayList containing student names, specifically showing that remove returns the removed element. Choice B is correct because the remove(1) method removes and returns the element at index 1, which is "Ben". Choice D is incorrect due to a common misconception that remove returns the index rather than the removed element itself. To help students, emphasize that remove has two forms: remove(int index) returns the removed element, while remove(Object o) returns a boolean. Practice using the return value of remove in assignments to understand this behavior.