0% found this document useful (0 votes)
43 views

AP CSA Unit 7 Array Lists

The document contains a series of programming questions related to the use of ArrayLists in Java, including class definitions, method implementations, and code segments. It covers topics such as initializing ArrayLists, modifying elements, searching algorithms, and removing elements based on specific criteria. Each question presents multiple-choice answers, requiring the reader to identify the correct implementation or output.

Uploaded by

kaavya.majumder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

AP CSA Unit 7 Array Lists

The document contains a series of programming questions related to the use of ArrayLists in Java, including class definitions, method implementations, and code segments. It covers topics such as initializing ArrayLists, modifying elements, searching algorithms, and removing elements based on specific criteria. Each question presents multiple-choice answers, requiring the reader to identify the correct implementation or output.

Uploaded by

kaavya.majumder
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit 7: ArrayList

Note: Please retain the original question numbers as it is in your answer sheet.
1. Consider the following Fraction class.

public class Fraction

private int nurn;


private int denom;

public Fraction{in~ n, int d)

nurn = n;
denom = d;

public int getNum()

return num;

public int getDenom()

return denom;

public void setNum(int n)

nurn =n;

public void setDenom(int d)

denom d;

public String toString()

return num + "/" + denom;

Which of the following would correctly initialize an ArrayList of Fraction objects with size 3?
ArrayList<Fraction> fractions= new ArrayList<Fraction>();
fractions[OJ new Fraction(3,5);
A
fractions[l] new Fraction(4,9);
fractions[2] new Fraction(S,7);

Fraction a new Fraction(3,5);


Fraction b new Fraction(4,9);
B Fraction c new Fraction(S,7);

ArrayList<Fraction> fractions {a,b,c};

ArrayList<Fraction> fractions= new ArrayList<Fraction>();


fractions.add(new Fraction(3,5));
C fractions.add(new Fraction(4,9));
fractions.add(new Fraction(S,7));

Fraction a= new Fraction(3,5);


Fraction b = new Fraction(4,9);
D Fraction c = new Fraction(S,7);

ArrayList<Fraction> fractions =new ArrayList<Fraction>(a,b,c);

Fraction a new Fraction(3,5);


Fraction b = new Fraction(4,9);
Fraction c = new Fraction(S,7);

E
ArrayList<Fraction> fractions =new ArrayList<Fraction>();
fractions.set(O,a);
fractions.set(l,b);
fractions.set(2,c);
2. Consider the following code segment.

ArrayList<String> line= new ArrayList<String>();


line. add ("Adam") ;
line.add("Bob");
line.add("Cindy");
line.add(l, "Dana");
line.set(2, "Ed");
Strings= line.remove(l);
line.add(line.remove(l));

System.out.println(s);
system.out.println(line);

What is displayed after the code segment is executed?

Dana
A
[Cindy, Adam, Ed]

Dana
B
[Adam, Cindy, Ed]

Dana
C
[Adam, Ed]

Dana
D
[Adam, Bob, Cindy, Ed]
Dana
E
[Adam, Ed, Cindy, Dana)
3. A method is to be implemented called removeBigStrings. The method accepts an ArrayList of String objects and returns an ArrayLi st of String objects
which is equivalent to the parameter ArrayList with all Strings of length six or more removed from the list.

Note that the original Ar rayList can be altered (or not) as long as the Ar rayList returned is appropriate.

Forexample,removeBigStrings(["the", "words", "encyclopedia", "baseball", "golf"]) returns ["the", "words", "golf"].

Which of the following options implement the method appropriately?

public ArrayList<String> removeBigStrings(ArrayList<String> words)

for (int i O; i <words.size(); i++)

if (words.get(i) .length()>=6)

words.remove(i);

return words;

II.

public ArrayList<String> removeBigStrings(ArrayList<String> words)

int index= O;
while (index< words.size())

if (words.get(index) .length() >= 6)

words.remove(index);

else

index++;

return words;

111.
public ArrayList<String> removeBigStrings(ArrayList<String> words)

ArrayList<String> shorts= new ArrayList<String>();


for (inti= O; i < words.size(); i++)

if (words.get(i) .length() < 6)

shorts.add(words.get(i));

return shorts;

A llonly.

B landll.

C I and Ill.

D II and Ill.

E 1,11,andlll.
4. Consider the following code segment.

ArrayList<Integer> ints = new ArrayList<Integer>();


ints.add(l);
ints.add(S);
ints.add(7);
ints.add(2, 9); //2 is the index and 9 is the value

int a= ints.set(l,8);
int b ints.remove(3);
int c = ints.get(2);

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(ints);

What is printed after the code segment is executed?

7
A
9

[1, 8, 9]

3
B
9

[l, 8, 9]
5

7
C
9

[l, 8, 9]

7
D
9

[l, 8, 5, 7]

E There will be an out-of-bounds exception and nothing will be printed.


5. For a linear or sequential search, the algorithm checks each element in order until the desired value is found or all elements in the array or ArrayList have been
checked.

Which of the following does not implement the sequential search appropriately?

Note that the method should return the index in which the target is found. If there are multiple indexes where the target is found, any of these indexes can be
returned. The method should return -1 if the target is never found in the list.

public int sequentialSearch(ArrayList<Integer> ints, int target)

for (int i =O; i < ints.size(); i++)

if {incs.get{i) target)
A
return i;

return -1;

public int sequentialSearch{ArrayList<Integer> ints, int target)

int index= -1;


for {int i =O; i < ints.size(); i++)

if (ints.get(i) target)
B
return i;

return index;
public int sequentialSearch(ArrayList<Integer> ints, int target)

index = -1;
for (int i =0; i < ints.size(); i++)

if (im::s.gei::(i) target)
C
index i;

return index;

public int sequentialSearch(ArrayList<Integer> ints, int target)

int index= O;
while (index< ints.size())

if (ints.get(index) target)
D
return index;

index++;

return index;
public int sequentialSearch(ArrayList<Integer> ints, int target)

int index= O;
while (index< ints.size())

if (ints.get(ir.dex) target)
E
return index;

index++;

return -1;

6. Consider the following code segment.

ArrayList<String> words= new ArrayList<String>();


words.add("apple");
words.add("banana");
words.add("card");
words.add("dirt");
words.add("egg");

Which of the fol lowing expressions will appropriately swap the words banana and dirt in the list?
words.set(2,words.get(4));
A words.set(4,words.get(2));

words.set(l,words.get(3));
B words.set(3,words.get(l));

String x = words.set(l,words.get(3));
C words.set(3,x);

Strings= words.get(l);
D words.add(l,words.get(3));
words.add(3,s);

String temp= words.remove(l);


String temp2 = words.remove(3);
E
words.add(l,temp2);
words.add(3,temp);
7. Consider the following search methods.

public int searchOne(int[J nums, int target)

for (inti= O; i < nums.length; i++)

if (nums[i] -- target)

return i;

return -1;

public int searchTwo(int[] nums, int target)

int index= -1;


for (inti= O; i < nums.length; i++)

if (nums[i] -- target)

index= i;

}
return index;

Which of the following would result in a and b having different values?


int[) array {2, 5, 8, 5, 11);

int find 2;
A
int a searchone(array, find);

int b searchTwo(array, find);

int[) array {2, 5, 8, 5, 11);

int find 11;


B
int a searchOne(array, find);

int b searchTwo(array, find);

int[] array {2, 5, 8, 5, 11);

int find 5;
C
int a= searchOne(array, find);

int b searchTwo(array, find);

int[] array {2, 5, 8, 5, 11);

int find 7;
D
int a = searchone(array, find);

int b searchTwo(array, find);

int[] array {2, 5, 8, 5, 11);

int find 8;
E
int a= searchOne(array, find);

int b searchTwo(array, find);


8. A method called remove Evens is to be implemented. The method should accept an ArrayList of integers and remove all even numbers from the list.

For example, if anArrayList called values holds [2, 3, 4, 6, 5, 8, 7] and a call to removeEvens (values) is made, theArrayList values should hold
[ 3, 5, 7] upon completion.

Which of the following methods would NOTwork appropriately?

public void removeEvens{ArrayList<Integer> ints)

for (int i O; i < ints.size(); i++)

if (ints.get{i)%2==0)
A
ints.remove(i);

public void removeEvens(ArrayList<Integer> ints)

for (int i O; i < ints.size(); i++)

if (in~s.ge~(i)%2==0)
B
ints.remove(i);
i--;

public void removeEvens(ArrayList<Ihteger> ints)

ints.size()-1; i >=0; i--)

if (ints.get(i)%2==0)
C
ints.remove(i);
public void removeEvens(ArrayList<Integer> ints)
{
int index= O;
while (index< ints.size())

if (ints.get(index)%2==0)
{
D ints.remove(index);
}
else
{
index++;

public void removeEvens(ArrayList<Integer> ints)


{
int index= ints.size() -1;
while (index>= 0)

if (ints.get(index)%2==0)
E {
ints.remove(index);
}
index--;
9. Suppose there are twoArrayLists, a list of pre-test scores and a list of post-test scores. We want to implement a method that will return the index of the student
who showed the greatest improvement from pre-test to post-test.

For example:

• greatestimprovement([66, 70, 80], [70, 75, 83J)returnstsincestudent1improvedby5points.

• greatest Improvement ( [ 65, 70, 72], ( 64, 66, 70)) returns 0 since student O improved by ·1 points.

Which of the following implements the method appropriately?

Assume that the twoArrayLists will have the same number of elements and there will be no ties for the highest improvement.

public int greatestimprovement(ArrayList<Integer> pre, ArrayList<Integer> post)

int index= O;
int high posc.get(O) - pre.get(O);
for (inti= l; i < pre.size(); i++)

int improvement post.get(il - pre.get(il;


A
if (improvement> high)

index i;

return index;

public int greatestimprovement(ArrayList<Integer> pre, ArrayList<Integer> post)

int index= O;
int high O;
for (inti= O; i <pre.size(); i++)

int improvement post.get(i) - pre.get(i);


B if (improvement> high)

high= improvement;
index= i;

return index;
public int greatestirnprovement(ArrayList<Integer> pre, ArrayList<Integer> post)

int index= O;
int high post.get(O) - pre.get(O);
for (inti= 1; i < pre.size(); i++)

int improvement post.get(i) - pre.get(i);


C if (improvement> high)

high= improvement;
index= i;

return i;

public int greatestimprovement(ArrayList<Integer> pre, ArrayList<Integer> post)

int index= O;
int high post.get(O) - pre.get(O);
for (inti= 1; i <pre.size(); i++)

int improvemenc post.gec(i) - pre.get(i);


D if (irnprovemenc > high)

high= improvement;
index= i;

return high;
public int greatestimprovement(ArrayList<Integer> pre, ArrayList<Integer> post)

int index= 0;
int high post.get(O) - pre.get(O);
for (inti= 1; i < pre.size(); i++)

int improvement post.get(i) - pre.get(i);


E if (improvement> high)

high= improvement;
index= i;

return index;

10. A method called swap is to be implemented. The method will accept an ArrayList of doubles and swap the values of the first and last elements in the list.

For example, if an ArrayList of doubles called dubs is [ 1. 1, 2. 2, 3. 3, 4. 4, 5. 5] and a call to swap {dubs) is made, then dubs would hold the values
[ 5 . 5, 2 . 2 , 3 . 3, 4 . 4 , 1 . 1 J after the method is executed.

Which of the following would implement the swap method appropriately?

public void swap(ArrayList<Double> d)

double a= d.remove(O);
A double b = d.remove(d.size()-1);
d.set(O,b1;
d.add(a);

public void swap(ArrayList<Double> d)

double a= d.remove(O);
B double b = d.remove(d.size()-1);
ct.add (0, bl;
d.add(a);
public void swap(ArrayList<Double> d)

double a= d.set(O, d.remove(d.size()-1));


C double b = d.set(d.size()-1, d.remove(O));
d.add(O,b);
d.add(a);

public void swap(ArrayList<Double> d)

double a= d.get(O);
D double b = d.get(d.size()-1);
d.add(O,b);
d.add(a);

public void swap(ArrayList<Double> d)

E d.add(d.remove(O));
d.add(O,d.remove(d.size()-1));
11. We want to implement a method called averageWordLength. The method will accept an ArrayList of String objects and return the average length of all the
words as a decimal.

For example, averageWordLength { ["cat", "table", "sixty" J) returns 4. 333 since (3 + 5 + 5)/3 = 4.333.
Which of the following code segments does NOT work appropriately?

public double averageWordLength(ArrayList<String> words)

double sum O;
for (int i 0; i <words.size(); i++)
A
sum+= words.get(i) .length();

return sum/words.size();

public double averageWordLength(ArrayList<String> words)

double sum O;
for (int i O; i <= words.size(); i++)
B
sum+= words.get(i) .length();

return sum/words.size();

public double averageWordLength(ArrayList<String> words)

double sum = O;
for (String word: words)
C
sum+= word.length();

return sum/words.size();
public double averageWordLength(ArrayList<String> words)

int sum= O;
for (inti= words.size()-1; i >=O; i--)
D
sum+= words.get(i) .length();

return (double)sum/words.size();

public double averageWordLength(ArrayList<String> words)


{

double sum= 0;
int index= O;
while (index< words.size())
E {

sum+= words.get(index) .length();


index++;
}

return sum/words.size();
}
12. Consider the following method below.

public void selectionSort(ArrayList<Integer> ints)

for (int i O; i < ints. size() -1; i++)

int min= ints.get(i);


int minindex = i;

for (int j i + 1; j < ints.size(); j++)

if (ints.get(j) < min)

min= ints.get(j);
minindex = j;

int valueAtI = ints.get(i);


int valueAtMinindex = ints.get(minindex);
ints.set(i, valueAtMinindex);
ints.set(minindex, valueAtI);

If the ArrayList list was initalized to hold values [ 8, 3, 5, 1, 4, 2 J. what would the values of list be after 2 passesof the outer loop if a call to
selectionsort (list) was made?

A c1, 2, 5, 8, 4, 3J

B c1, 2, s, 3, 5, 4, 21

C c2, 3, 4, 1, 5, 8J

D £3, 1, 4, 2, 5, 8J

E c1, 3, 5, 8, 4, 2J
13. ArrayList<String> list= new ArrayList<String>();
list.add("add");
list.add("bad");
list.add("cat");
list.add("dad");

int size= list.size();

list.remove(O);
list.remove(size-1);

System.out.println(list);

What will be displayed after the code segment is executed?

A [bad, dad]

B [bad, cat]

C [add, dad]

D [add, cat]

E Nothing. An out-of-bounds exception will occur.


14. Consider the following code segment.

ArrayList<Integer> ints = new ArrayList<Integer>();


ints.add(O);
ints.add(l);
ints.add(2);
ints.add(3);
ints.add(4);

int size= ints.size();

for (inti= O; i < size; i++)

ints.remove(i);

System.out.println(ints.size());

What will be printed when the code segment is run?

A o

B 2

C s

D Nothing will be printed. A compile/syntax error will occur.

E Nothing will be printed. A run-time exception will occur.


15. Considerthe followingmethods.

public void selectionSort(ArrayList<Integer> arr)

for (int i O; i < arr.size(); i++)


{

int pos = i;
for (int j = i; j < arr.size() j++)

if (arr.get(j) < arr.get(pos))

pas j;

int min= arr.get(pos);


arr.set(pos, arr.get(i))
arr.set(i, min);

public void insertionSort(ArrayList<Integer> nums)

for (int j 1; j < nums.size(); j++)

int key= nums.get(j);


inti j-1;
while (i > -1) && ( nums.get(i) >key) )

nums.set(i+l, nums.get(i));
i--;

nums.set(i+l,key);

If the ArrayList containing the values [ 2, 5, 7, 3, 6] was sorted using the selectionsort and insertionSort methods, which of the following would
be true about the number of comparisons would be made for each method?

Note that a comparison is defined as comparing two values in the list, using operators such as<,>,<=,>=, or ! =.
selectionSort will make lOcomparisons.
A
insertionsort will make 4 comparisons.

selectionsort will make 15 comparisons.


B
insertionsort will make 4comparisons.

selectionsort will make lOcomparisons.


C
insertionsort will make lOcomparisons.

selectionSort will make lOcomparisons.


D
insertionsort will make 7 comparisons.

selectionsort will make 15 comparisons.


E
insertionsort will make 7 comparisons.
1. Consider the following code segment:

ArrayList list = new ArrayList( );


list.add(“A”);
list.add(“B”);
list.add(0, “C”);
list.add(“D”);
list.set(2, “E”);
list.remove(1);
System.out.println(list);

What is printed as a result of executing the code segment?

(A) [A, B, C, D, E]
(B) [A, B, D, E]
(C) [C, E, D]
(D) [A, D, E]
(E) [A, C, D, E]

2. Consider the following data fields and method:


private ArrayList letters;
// precondition: letters.size( ) > 0
// letters contains String objects
public void letterRemover( )
{
int i = 0;
while (i < letters.size( ))
{
if (letters.get(i).equals(“A”))
letters.remove(i);
i++;
}
}
Assume that ArrayList letters originally contains the following
String values:

[A, B, A, A, C, D, B]

What will letters contain as a result of executing letterRemover(


)?

(A) [A, B, A, A, C, D, B]
(B) [B, C, D, B]
(C) [B, A, C, D, B]
(D) [A, B, A, C, D, B]
(E) [A, A, B, C, D, B, D]

-3. Consider the following method:


private ArrayList myList;
// precondition: myList.size( ) > 0
// myList contains String objects
public void myMethod( )
{
for (int i = 0; i < myList.size( ) – 1; i++)
{
myList.remove(i);
System.out.print(myList.get(i) + “ ”);
}
}

Assume that myList originally contains the following String values:

[A, B, C, D, E]

What will be printed when the method above executes?

(A) A B C D E
(B) A C E
(C) B D E
(D) B D
(E) Nothing will be printed due to an IndexOutOfBoundsException.

-4. Consider the following code segment:


ArrayList list = new ArrayList( );
for (int i = 1; i <= 8; i++)
{
list.add(new Integer(i));
}
for (int j = 1; j < list.size( ); j++)
{
list.set(j / 2, list.get(j));
}
System.out.println(list);

What is printed as a result of executing the code segment?

(A) [2, 4, 6, 8, 5, 6, 7, 8]
(B) [1, 2, 3, 4, 5, 6, 7, 8]
(C) [1, 2, 3, 4]
(D) [1, 2, 3, 4, 1, 2, 3, 4]
(E) [2, 2, 4, 4, 6, 6, 8, 8]
Basic Level
1. Assume that cities is an ArrayList<String> that has been correctly
constructed and populated with the following items.
["Oakland", "Chicago", "Milwaukee", "Seattle", "Denver", "Boston"]

Consider the following code segment.


cities.remove(2);
cities.add("Detroit");
cities.remove(4);
cities.add("Cleveland");

What items does cities contain after executing the code segment?
(A) ["Cleveland", "Detroit", "Oakland", "Chicago", "Seattle", "Denver", "Boston"]
(B) ["Oakland", "Milwaukee", "Seattle", "Boston", "Detroit", "Cleveland"]
(C) ["Oakland", "Chicago", "Seattle", "Boston", "Detroit", "Cleveland"]
(D) ["Oakland", "Milwaukee", "Denver", "Boston", "Detroit", "Cleveland"]
(E) ["Oakland", "Chicago", "Seattle", "Denver", "Detroit", "Cleveland"]

2. Consider the following code segment.


ArrayList<String> subjects = new ArrayList<String>();
subjects.add("French");
subjects.add("History");
subjects.set(1, "English");
subjects.add("Art");
subjects.remove(1);
subjects.set(2, "Math");
subjects.add("Biology");
System.out.println(subjects);

What is printed as a result of executing the code segment?


(A) [French, English, Math, Biology]
(B) [French, Art, Biology]
(C) [French, English, Art, Math, Biology]
(D) [French, Math, Biology]
(E) IndexOutOfBoundsException

Questions 3-6 refer to the following information.


Array arr has been defined and initialized as follows
int[] arr = {5, 3, 8, 1, 6, 4, 2, 7};

3. Which of the following shows the elements of the array in the correct
order after the first pass through the outer loop of the Insertion Sort
algorithm?
(A) 1 5 3 8 6 4 2 7
(B) 1 3 8 5 6 4 2 7
(C) 5 3 7 1 6 4 2 8
(D) 3 5 8 1 6 4 2 7
(E) 1 2 3 4 5 6 7 8

4. Which of the following shows the elements of the array in the correct
order after the fourth pass through the outer loop of the Insertion Sort
algorithm?
(A) 1 3 5 6 s 4 2 7
(B) 1 2 3 4 6 5 8 7
(C) 1 2 3 4 5 6 7 8
(D) 3 5 8 1 6 4 2 7
(E) 1 2 3 4 5 6 8 7

5. Which of the following shows the elements of the array in the correct
order after the first pass through the outer loop of the Selection Sort
algorithm?
(A) 1 2 3 5 6 4 8 7
(B) 1 3 8 5 6 4 2 7
(C) 1 3 5 8 6 4 2 7
(D) 1 5 3 8 6 4 2 7
(E) 5 3 1 6 4 2 7 8

6. Which of the following shows the elements of the array in the correct
order after the fourth pass through the outer loop of the Selection Sort
algorithm?
(A) 3 1 4 2 5 6 7 8
(B) 3 1 4 2 5 8 6 7
(C) 1 2 3 4 5 6 7 8
(D) 1 2 3 4 5 8 6 7
(E) 1 2 3 4 6 5 8 7

Advanced Level
7. Consider the following code segment.
int total= 3;
ArrayList<Integer> integerList = new ArrayList<Integer>();
for (int k = 7; k < 11; k++)
{
integerList.add(k + 3);
}
for (Integer i : integerList)
{
total+= i;
if (total% 2 == 1)
{
total-= 1;
}
}
System.out.println(total);
What is printed as a result of executing the code segment?
(A) 34
(B) 37
public void insertionSort(int[] arr)
{
for (inti= 1; i < arr.length; i++)
{
int key= arr[i];
int j = 1 - 1;
while (j >= O && /*condition*/)
{
arr[j + 1] = arr[j];
J- - ;
}
arr[j + 1] = key;
}
}
Which of the following can be used to replace /* condition */ so that
insertionSort will work as intended?
(A) arr [i] > key
(B) arr [j] > key
(C) arr[i + 1] > key
(D) arr[j + 1) > key
(E) arr[i - 1] > key

10. Determine if the numbers in an ArrayList are always increasing.


Write a method that determines if the values in an ArrayList are
always increasing. The method takes one parameter: an ArrayList of
Integer. The method returns true if the elements in the array are
continually increasing and false if the elements are not continually
increasing.
ArrayList<Integer> listOfintegers =/*contains values from table below */
boolean result= isincreasing(listOfintegers); // result is true

For example, if the ArrayList passed to the method is:


34 35 36 37 38 10 12 43 51 52

then the value true is returned


public boolean isincreasing(ArrayList<Integer> arr)
{
// Write the implementation
}
11. Determine if the rate is increasing.
If a stock price is increasing, that means the value of it is going up. If
the rate that the stock price is increasing is increasing, it means that the
price is rising at a faster rate than it was the day before. Write a method
that determines if the rate at which a stock price increases is increasing.
The method has one parameter: an ArrayList of Double values that
represent the stock prices. Return true if the rate that the stock is
increasing is increasing, and false if the rate is not increasing.
ArrayList<Double> prices= I* contains the values from the table below *I
boolean result= rateisincreasing(prices); II result is true

For example, if stockPrices contains the following:


+2.1 +3.1 +5.1 +8.1 +12.1 +21.1 +24.1
~~~~~~~
10.1 1 12.2 1 15.3 1 20.4 1 28.5 1 4o.6 1 61.7 1 85.8

then the value true is returned


public boolean rateisincreasing(ArrayList<Double> stockPrices)
{
// Write the implementation
}

12. Count the empty lockers.


A high school has a specific number of lockers so students can store
their belongings. The office staff wants to know how many of the
lockers are empty so they can assign these lockers to new students.
Consider the following classes and complete the implementation for the
countEmptyLockers method.
public class Locker
{
private boolean inUse;

public boolean isinUse()


{ return inUse; }

public void setinUse(boolean isinUse)


{ inUse = isinUse; }

public class School


{
private ArrayList <Locker> lockerlist;

/* Additional implementation not shown */

/**
* This method counts the number of empty lockers in the list of lockers.
* @param lockers the list of Locker objects
* @return the number of empty lockers
* PRECONDITION: No object in the list lockers is null
* (every locker has a true/false value)
*/
public static int countEmptyLockers(ArrayList <Locker> lockers)
{
/* to be implemented*/
}

Answers and Explanations


Bullets mark each step in the process of arriving at the correct solution.

1. The answer is E.
• Let’s picture the contents of our ArrayList in a table:

• After the remove at index 2, we have this (notice how the indices
have changed):

• After adding Detroit, we have:

You might also like