ArrayList in AP Computer Science A
When trying to retrieve an element from an ArrayList at a certain index, which exception is thrown if the index is out of bounds?
NoSuchElementException
IllegalArgumentException
IndexOutOfBoundsException
ArithmeticException
What is an advantage of using separate methods within a class to perform different tasks in a program?
Reduces the amount of memory required by the program.
Eliminates the need for data structures like arrays or ArrayLists.
Enhances readability and maintainability of the code.
Increases the overall execution speed of the program.
When removing elements from an ArrayList within a loop, which type of loop reduces the likelihood of encountering index out-of-bounds errors or skipping elements?
Standard forward iteration with a while loop
Recursive method calls
Enhanced for (for-each) loop
Backward iteration with a for loop
What does the get(int index) method of an ArrayList return?
An updated list without the element at that position.
True if the element exists, false otherwise.
The number of elements in the list.
The element at the specified index in the list.
Given an abstract class 'Shape' with subclasses 'Circle', 'Rectangle', etc., if you have an ArrayList
Store distinct ArrayLists for each shape type separately.
Use instanceOf before casting every object from shapes.
Implement polymorphic methods within each Shape subclass.
Cast all objects from shapes array into their respective classes.
How can you determine if an ArrayList named “inventory” contains a specific item known as “widget”?
inventory.indexOf("widget") >=0
inventory.contains("widget")
“widget”.existsIn(inventory)
inventory.hasItem("widget")
How do you retrieve an element from an ArrayList at a given position?
Using clear().
Using remove(int index).
Using add(E e).
Using get(int index).

How are we doing?
Give us your feedback and let us know how we can improve
How do you retrieve the element at the third position of an ArrayList named "myList"?
myList.elementAt(3)
myList.get(2)
myList[2]
myList.get(3)
Given ArrayList<String> words = new ArrayList<>(Arrays.asList("apple","banana","cherry","date"));, which modification would reverse their order to [“date”,”cherry”,”banana”,”apple”] using minimal calls to ArrayList methods?
Collections.reverse(words);
for(int i=0;i<words.size()/2;i++){String temp=words.get(i);words.set(i,words.get(words.size()-i-1));words.set(words.size()-i-1,temp);}
Collections.sort(words, Comparator.reverseOrder());
words.sort(Collections.reverseOrder());
After executing ArrayList<String> colors = new ArrayList<>(Arrays.asList("red", "green", "blue")); , what does colors.get(1) return?
red
blue
IndexOutOfBoundsException
green