ArrayList Methods - AP Computer Science A

  1. 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); } }
    Answer: Elixir 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.
  2. 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)); } }
    Answer: Bea 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.
  3. 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 } }
    Answer: 2 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.
  4. In this game inventory, what is printed after removing index 2? import java.util.ArrayList; public class Game { public static void main(String[] args) { ArrayList<String> bag = new ArrayList<String>(); bag.add("potion"); bag.add("key"); bag.add("map"); bag.add("coin"); bag.remove(2); // remove "map" System.out.println(bag.get(2)); } }
    Answer: coin 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 removing an element at index 2 and then accessing the new element at that same index. Choice B is correct because after removing "map" at index 2, "coin" (previously at index 3) shifts left to index 2, so get(2) returns "coin". Choice A is incorrect due to the misconception that the removed element somehow remains accessible at its original position. To help students, emphasize that removing an element causes all subsequent elements to shift left by one position. Visual diagrams showing the ArrayList before and after removal help students understand this shifting behavior.
  5. 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)); } }
    Answer: Noah 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.
  6. 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()); } }
    Answer: 2 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.
  7. How would you modify the student list so "Dee" becomes the first element?import java.util.ArrayList; public class RosterDemo { public static void main(String[] args) { ArrayList<String> roster = new ArrayList<String>(); roster.add("Ari"); roster.add("Bo"); roster.add("Cy"); // make "Dee" index 0 } }
    Answer: roster.add(0, "Dee"); 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 requires inserting a new element at the beginning of an ArrayList. Choice A is correct because add(0, "Dee") inserts "Dee" at index 0, shifting all existing elements to the right. Choice B is incorrect because set would replace "Ari" rather than making "Dee" the first element while keeping all others. To help students, emphasize the difference between add(index, element) for insertion and set(index, element) for replacement. Practice identifying when each method is appropriate based on whether you need to preserve existing elements.
  8. 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 } }
    Answer: Key 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.
  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()); } }
    Answer: 3 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. 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 } }
    Answer: Ben 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.
  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 } }
    Answer: 7 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.
  12. 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)); } }
    Answer: Emma 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.
  13. In this social media app, what is the output after remove("Sam") executes? import java.util.ArrayList; public class Friends { public static void main(String[] args) { ArrayList<String> friends = new ArrayList<String>(); friends.add("Sam"); friends.add("Tia"); friends.add("Sam"); friends.remove("Sam"); // removes first matching element System.out.println(friends.get(0)); } }
    Answer: Tia 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 and remove providing essential functionality. The given code snippet demonstrates the remove method with a String parameter, which removes the first occurrence of that element from the ArrayList. Choice B is correct because remove("Sam") removes the first "Sam" at index 0, causing "Tia" to shift to index 0, making get(0) return "Tia". Choice A is incorrect due to the misconception that remove("Sam") removes all occurrences or that elements don't shift after removal. To help students, emphasize that remove(Object) removes only the first matching element and that remaining elements shift left. Practice distinguishing between remove(int index) and remove(Object element) methods.
  14. 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)); } }
    Answer: 2 Bananas 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.
  15. 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. } }
    Answer: authors.set(1, "Emma"); 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.
  16. 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" } }
    Answer: set(index, value) 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.
  17. In this library catalog, what is printed by the size call after two adds?import java.util.ArrayList; public class SizeDemo { public static void main(String[] args) { ArrayList<String> catalog = new ArrayList<String>(); catalog.add("The Hobbit"); catalog.add("Fahrenheit 451"); System.out.println(catalog.size()); } }
    Answer: 2 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 after adding elements to an ArrayList. Choice C is correct because after adding two elements ("The Hobbit" and "Fahrenheit 451"), the size() method returns 2, representing the number of elements in the list. Choice A is incorrect due to a common misconception that size returns the highest index rather than the count of elements. To help students, emphasize that size() returns the number of elements, not the last index (which would be size()-1). Practice using size() in loops and understanding the relationship between size and valid indices.
  18. 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)); } }
    Answer: Eggs 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.
  19. 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); } }
    Answer: Ben 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.
  20. In this social media message list, what is printed after add and get?import java.util.ArrayList; public class MessagesDemo { public static void main(String[] args) { ArrayList<String> messages = new ArrayList<String>(); messages.add("hi"); messages.add("sup"); messages.add(1, "lol"); // inserts at index 1 System.out.println(messages.get(1)); } }
    Answer: lol 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 to insert an element at a specific position. Choice C is correct because add(1, "lol") inserts "lol" at index 1, shifting "sup" to index 2, so get(1) returns "lol". Choice B is incorrect due to a common misconception that add with an index replaces the existing element rather than inserting and shifting. To help students, emphasize the difference between add(index, element) which inserts and shifts, versus set(index, element) which replaces. Practice visualizing how elements shift right when using add with an index.

Start this quiz