Practice a College Board-style Data Analysis free response on arrays and ArrayList — averaging, counting, and safe removal. Write your Java, then reveal the model answer to see exactly what earns each point.
Free Response Question · Unit 4 · Data Collections
You are given an int[] scores (an array of test scores) and an ArrayList<Integer> nums. Write methods that process these collections. This mirrors the exam’s Data Analysis question.
A
Write a method average(int[] scores) that returns the average of the values as a double.
✓ Model answer
public double average(int[] scores) {
int sum = 0;
for (int s : scores) {
sum += s;
}
return (double) sum / scores.length;
}
Why it scores: Traverses every element, accumulates a sum, and divides by scores.length using a double cast for a real average. Using integer division (sum / scores.length) would truncate and lose the point.
B
Write a method countAbove(int[] scores, int cutoff) that returns how many scores are strictly greater than cutoff.
✓ Model answer
public int countAbove(int[] scores, int cutoff) {
int count = 0;
for (int s : scores) {
if (s > cutoff) {
count++;
}
}
return count;
}
Why it scores: Traverses the array, uses a strict > comparison against cutoff, increments a counter inside the if, and returns it. Using >= (not strict) would miscount.
C
Write a method removeEvens(ArrayList<Integer> nums) that removes every even value from nums. Explain how you avoid skipping elements.
✓ Model answer
public void removeEvens(ArrayList<Integer> nums) {
int i = 0;
while (i < nums.size()) {
if (nums.get(i) % 2 == 0) {
nums.remove(i);
} else {
i++;
}
}
}
When an element is removed, later elements shift left, so I do not increment i after a removal — the next element has moved into index i. I only advance i when I keep an element.
Why it scores: Correctly removes even values and avoids the skip bug by not advancing the index after a removal (or equivalently looping backwards). Blindly using a for loop with i++ after remove would skip elements and lose credit.
How to score points on AP CSA free response
Traverse fully — index 0 to length−1 for arrays, 0 to size()−1 for lists.
Cast to double for real averages so integer division doesn’t truncate.
Match the comparison to the wording (strict > vs. >=).
Don’t skip after removal — don’t advance the index, or loop backwards.
Use .length for arrays, .size() for ArrayList — never mix them.