ArrayList Methods

Help Questions

AP Computer Science A › ArrayList Methods

Questions 1 - 10
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());

    }

}

Use students.remove();

Use students.remove(0);

Use students.delete(0);

Use students.remove(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 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.

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"

    }

}

add(value, index)

set(index, value)

size(index, value)

get(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.

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));

    }

}

Ava

Ben

Bea

Cal

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.

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));

    }

}

Sula

1

It

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 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.

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());

    }

}

0

1

2

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 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.

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

    }

}

qty.add(0, 20);

qty.set(0, 20);

qty.remove(0, 20);

qty.get(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.

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());

    }

}

0

2

3

4

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.

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);

    }

}

Potion

Shield

Elixir

Sword

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.

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));

    }

}

3 Carrots

2 Bananas

2 Carrots

3 Bananas

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.

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

    }

}

0

1

2

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 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.

Page 1 of 3