AP CSA Unit 7 Array Lists
AP CSA Unit 7 Array Lists
Note: Please retain the original question numbers as it is in your answer sheet.
1. Consider the following Fraction class.
nurn = n;
denom = d;
return num;
return denom;
nurn =n;
denom d;
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);
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.
System.out.println(s);
system.out.println(line);
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.
if (words.get(i) .length()>=6)
words.remove(i);
return words;
II.
int index= O;
while (index< words.size())
words.remove(index);
else
index++;
return words;
111.
public ArrayList<String> removeBigStrings(ArrayList<String> words)
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.
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);
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]
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.
if {incs.get{i) target)
A
return i;
return -1;
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;
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;
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);
if (nums[i] -- target)
return i;
return -1;
if (nums[i] -- target)
index= i;
}
return index;
int find 2;
A
int a searchone(array, find);
int find 5;
C
int a= searchOne(array, find);
int find 7;
D
int a = searchone(array, find);
int find 8;
E
int a= searchOne(array, find);
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.
if (ints.get{i)%2==0)
A
ints.remove(i);
if (in~s.ge~(i)%2==0)
B
ints.remove(i);
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++;
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:
• greatest Improvement ( [ 65, 70, 72], ( 64, 66, 70)) returns 0 since student O improved by ·1 points.
Assume that the twoArrayLists will have the same number of elements and there will be no ties for the highest improvement.
int index= O;
int high posc.get(O) - pre.get(O);
for (inti= l; i < pre.size(); i++)
index i;
return index;
int index= O;
int high O;
for (inti= O; i <pre.size(); i++)
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++)
high= improvement;
index= i;
return i;
int index= O;
int high post.get(O) - pre.get(O);
for (inti= 1; i <pre.size(); i++)
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++)
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.
double a= d.remove(O);
A double b = d.remove(d.size()-1);
d.set(O,b1;
d.add(a);
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.get(O);
D double b = d.get(d.size()-1);
d.add(O,b);
d.add(a);
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?
double sum O;
for (int i 0; i <words.size(); i++)
A
sum+= words.get(i) .length();
return sum/words.size();
double sum O;
for (int i O; i <= words.size(); i++)
B
sum+= words.get(i) .length();
return sum/words.size();
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();
double sum= 0;
int index= O;
while (index< words.size())
E {
return sum/words.size();
}
12. Consider the following method below.
min= ints.get(j);
minindex = j;
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");
list.remove(O);
list.remove(size-1);
System.out.println(list);
A [bad, dad]
B [bad, cat]
C [add, dad]
D [add, cat]
ints.remove(i);
System.out.println(ints.size());
A o
B 2
C s
int pos = i;
for (int j = i; j < arr.size() j++)
pas j;
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.
(A) [A, B, C, D, E]
(B) [A, B, D, E]
(C) [C, E, D]
(D) [A, D, E]
(E) [A, C, D, E]
[A, B, A, A, C, D, B]
(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]
[A, B, C, D, E]
(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.
(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"]
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"]
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
/**
* 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*/
}
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):