ArrayList Methods

Help Questions

AP Computer Science A › ArrayList Methods

Questions 1 - 10
1

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.

2

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

    }

}

2

5

7

9

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.

3

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

    }

}

Ben

Ava

Dee

Cy

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.

4

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.delete(0);

Use students.remove(1);

Use students.remove();

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

5

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

    }

}

Key

null

Potion

Map

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.

6

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 <u>"Orwell", "Emma", "Tolkien"</u>

        // Choose one line to replace Austen with Emma.

    }

}

authors.get(1, "Emma");

authors.add(1, "Emma");

authors.remove("Emma");

authors.set(1, "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.

7

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

    }

}

Milk

Bread

Eggs

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.

8

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

    }

}

Lia

1

Nia

Max

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.

9

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.

10

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

    }

}

Change to effects.set("Speed", "Strength");

Change to effects.get(2, "Strength");

Change to effects.add(2, "Strength");

Change to effects.set(1, "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.

Page 1 of 3