ArrayList in AP Computer Science A
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());
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 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.
After executing ArrayList<String> colors = new ArrayList<>(Arrays.asList("red", "green", "blue"));
, what does colors.get(1)
return?
red
blue
IndexOutOfBoundsException
green
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")
After executing this line of code on an existing populated ArrayList<Double> doublesList
, what will be its effect?:
java
doublesList.set(doublesList.indexOf(Collections.max(doublesList)), Math.PI);
Replaces the largest value in doublesList with (Math.PI).
Replaces all occurrences of (Math.PI) in doublesList with the maximum value found in it.
Adds (Math.PI) as a new item after every occurrence of max value found within doublesList.
Inserts (Math.PI) into doublesList without affecting any other items.

How are we doing?
Give us your feedback and let us know how we can improve
For given code snippet 'List
doubles = Doubles.Stream.generate(Math.random).limit(10).map(D->new BigDecimal(D.toString()).setScale(2, BigDecimal.RoundingMode.HALF_UP).doubleValue()).collect(Collectors.toList());
doubles = doubles.stream().mapToObj(BigDecimal::new).map(bd -> bd.setScale(2, RoundingMode.UP).doubleValue()).collect(Collectors.toList());
Doubles.replaceAll(BD->Double.parseDouble(String.format("%.2f", BD)));
Doubles.forEach(D->{BigDecimal BD=new BigDecimal(Double.toString(D));BD=BD.setScale(2,RoundingMode.HALF_UP);D=BD.doubleValue();});
Why are helper methods useful in a class that implements complex algorithms?
They allow variables to be shared freely between classes without encapsulation.
They enable Java programs to run on any platform without modification.
They directly increase the processing power of the computer.
They break down complexity into smaller, manageable parts.
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).