AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Arraylists Clear filter
Question 202
Question 203
String whose length is less than 3 from list.
public static void cleanup(ArrayList<String> list)
{
for (int i = 0; i < list.size(); i++)
{
if (list.get(i).length() < 3)
{
list.remove(i);
}
}
}
Given the initial list ["a", "bb", "ccc", "dd", "eeee", "ff", "gg"], what does list contain after a call to cleanup(list)?Main Solution
Solution:
Correct: D. Classic index-skipping bug: after removing, i still increments, skipping the element that shifts into position i. Trace: i=0 "a" remove → [bb,ccc,dd,eeee,ff,gg]; i=1 "ccc" keep; i=2 "dd" remove → [bb,ccc,eeee,ff,gg]; i=3 "ff" remove → [bb,ccc,eeee,gg]; i=4, size=4 so loop ends. Final: ["bb","ccc","eeee","gg"].
Answer: Correct: D
Answer images
Question 204
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++)
{
a.add(i * 3);
}
for (int i = 0; i < a.size(); i++)
{
if (i % 2 == 0)
{
a.set(i, a.get(i) + i);
}
}
System.out.println(a);
What is printed?Main Solution
Solution:
Correct: A. First loop fills [3, 6, 9, 12, 15, 18]. Second loop modifies only even indices by adding the index: i=0→3+0=3, i=2→9+2=11, i=4→15+4=19. Odd indices unchanged. Result: [3, 6, 11, 12, 19, 18].
Answer: Correct: A
Answer images
Question 205
SurveyResults analyzes survey scores. scores is a nonnull ArrayList, possibly empty, with every value in [0,100]. The constructor keeps the supplied list reference.
public class SurveyResults {
private java.util.ArrayList scores
public SurveyResults(java.util.ArrayList initial){scores=initial;}
// Implement countAbove and trim.
}| Operation | Result |
|---|---|
| Initial scores | [85,42,90,42,73,100,15] |
| countAbove(50) | 4 |
| trim(40,90) | [85,42,90,42,73] |
| then trim(50,80) | [73] |
(a) Implement countAbove(int threshold), returning the number of scores strictly greater than threshold.
(b) Implement trim(int minAccept,int maxAccept), removing scores outside the inclusive range [minAccept,maxAccept], while preserving the relative order of all remaining values. Assume 0 <= minAccept <= maxAccept <= 100. Handle empty lists, removal of every element, and consecutive removals.
Main Solution
Answer Key
(a)
public int countAbove(int threshold){
int count=0
for(int value:scores)if(value>threshold)count++
return count
}
(b)
public void trim(int minAccept,int maxAccept){
for(int i=scores.size()-1;i>=0;i--)
if(scores.get(i)maxAccept)scores.remove(i)
}Answer images
Question 206
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(2);
for (int i = 0; i < list.size(); i++)
{
if (list.get(i) == 2)
{
list.remove(i);
}
}
System.out.println(list);
What is printed when the code segment executes?Main Solution
Solution:
Answer: B. Removing from an ArrayList while iterating with a forward index is the classic "skip-one-then-next" bug. Trace: start [1, 2, 2, 3, 2], i = 0 keeps 1; i = 1 removes the first 2, list becomes [1, 2, 3, 2]; after the update i = 2 now points at 3 (the second 2 slid into index 1 and was skipped); i = 3 points at 2, removed, list becomes [1, 2, 3]; i = 4 is no longer less than size() = 3, loop exits. The final list is [1, 2, 3] — one 2 "survives" because of the skip.
Answer: Correct: B
Answer images
Question 207
int[] a but not for an ArrayList<Integer> list?Main Solution
Solution:
Answer: D. An array's length is exposed as a public final field named length, written without parentheses: a.length. An ArrayList exposes its element count through the method size(), written with parentheses; there is no .length field. Reading and writing by index are possible with both, but an array uses a[3] / a[3] = x while an ArrayList uses list.get(3) / list.set(3, x).
Answer: Correct: D
Answer images
Question 208
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
list.add(3, "Y");
list.remove(0);
System.out.println(list);
What is printed when the code segment executes?Main Solution
Solution:
Answer: C. Track the list after each operation. After the three appends: [A, B, C]. add(1, "X") inserts X at index 1, shifting B, C to the right: [A, X, B, C]. add(3, "Y") inserts Y at index 3, shifting C right: [A, X, B, Y, C]. remove(0) drops A, yielding [X, B, Y, C]. The trick is that add(index, obj) shifts existing elements rather than replacing them.
Answer: Correct: C
Answer images
Question 209
A linguist is studying word usage in a corpus. Given an ArrayList<String> of the words that appear in a document (in order, with possible repetitions), the linguist wants the list of most common words in that document.
Write a static method mostFrequent that, given such a list, returns a new ArrayList<String> containing every word whose number of occurrences in words equals the maximum number of occurrences attained by any word. In the returned list:
- each qualifying word appears exactly once, even if it occurs many times in
words; - the qualifying words appear in the order of their first occurrence in
words.
Two words are considered the same only if they are equal as strings (case-sensitive). The parameter words must not be modified. If words is empty, return an empty list.
Method Signature
public static ArrayList<String> mostFrequent(ArrayList<String> words)
Examples
| Input words | Returned list | Explanation |
|---|---|---|
| ["to","be","or","not","to","be"] | ["to","be"] | Both appear twice; "to" occurs first, then "be". |
| ["red","blue","red","green","blue","red"] | ["red"] | Max count is 3 ("red"); "blue" has count 2. |
| ["a","b","c","d"] | ["a","b","c","d"] | All words tie at count 1; order is preserved. |
| ["Cat","cat","Cat"] | ["Cat"] | Case-sensitive: "Cat" has count 2, "cat" has count 1. |
| [] | [] | Empty input → empty output. |
Main Solution
Sample Solution
public static ArrayList<String> mostFrequent(ArrayList<String> words)
{
ArrayList<String> distinct = new ArrayList<String>();
ArrayList<Integer> counts = new ArrayList<Integer>();
for (int i = 0; i < words.size(); i++)
{
String w = words.get(i);
int idx = -1;
for (int j = 0; j < distinct.size(); j++)
{
if (distinct.get(j).equals(w))
idx = j;
}
if (idx == -1)
{
distinct.add(w);
counts.add(1);
}
else
{
counts.set(idx, counts.get(idx) + 1);
}
}
int max = 0;
for (int i = 0; i < counts.size(); i++)
{
if (counts.get(i) > max)
max = counts.get(i);
}
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < distinct.size(); i++)
{
if (counts.get(i) == max)
result.add(distinct.get(i));
}
return result;
}
Scoring. The two parallel lists distinct and counts (populated in input order) encode both the frequency table and the first-occurrence order for free. A single pass over counts finds the maximum; a final pass emits every entry whose count matches. The original words is never written to. Returning an empty list when words is empty falls out of the algorithm (the max stays 0 and the final loop produces nothing).
Teacher Explanation
Education Coach Advice
Traps
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

