AP Computer Science A · Exam guide
AP Computer Science A Question 3 Explained: Data Analysis with an ArrayList
Question 3 asks you to process an ArrayList of objects. It is worth 9 points, and the same trap catches many students: removing items while moving forward through the list. The example below was compiled and run.
A sample question
A class Item has the methods getName() and getPrice() (a double). Write removeCheaper(ArrayList<Item> items, double limit), which removes every item whose price is less than limit, and total(ArrayList<Item> items), which returns the sum of the prices.
A full-credit solution
public static void removeCheaper(ArrayList<Item> items, double limit)
{
for (int i = items.size() - 1; i >= 0; i--)
{
if (items.get(i).getPrice() < limit)
{
items.remove(i);
}
}
}
public static double total(ArrayList<Item> items)
{
double sum = 0.0;
for (Item it : items)
{
sum += it.getPrice();
}
return sum;
}
Tested with prices 2.0, 5.0, 3.0 and 8.0 and a limit of 4.0: removeCheaper leaves two items, and total returns 13.0.
Why the loop runs backward
When you remove an item, every later item moves down one index. A forward loop then skips the item that slid into the removed spot. With prices 2.0, 3.0 and 5.0 and a limit of 4.0, a forward loop removes the 2.0, moves to index 1, and skips the 3.0, so it stays in the list. Looping from the end to the start avoids the problem. Another correct approach is a forward loop that does not advance i after a removal.
How the points are earned
| Piece | Where it appears |
|---|---|
| Declares the accumulator | double sum = 0.0; |
| Traverses the whole list | the backward loop covers every index, and the enhanced for visits every item |
| Tests the condition with the given method | items.get(i).getPrice() < limit |
| Updates correctly | items.remove(i); and sum += it.getPrice(); |
| Returns the right value | return sum; after the loop |
| Row | Points | What earns it |
|---|---|---|
| Correctness of the algorithm | 6 | Six one-point pieces: declares the needed variables; loops over the correct range; tests the stated condition; updates the right variable on each pass; handles edge cases; returns the right value at the right time. |
| Use of the Java API and syntax | 2 | One point for using the given methods and the ArrayList and array syntax correctly; one for Java that would compile, with matching headers and declared variables. |
| Program design and encapsulation | 1 | Uses the class's own methods and keeps data private, as the question's design requires. |
| Total | 9 |
ArrayList reminders
- size() gives the number of elements, get(i) reads one, set(i, x) replaces one, add(x) appends, and remove(i) deletes one.
- The list's type goes in angle brackets, as in ArrayList<Item>.
- An enhanced for loop is fine for reading, but never remove from the list inside it.
Common mistakes
- Removing in a forward loop and skipping elements.
- Using length instead of size(), or length() instead of size().
- Using [i] instead of get(i).
- Forgetting to call the getter and comparing the object to the number.
Frequently asked questions
What is Question 3 about?
Processing an ArrayList of objects: traversing it, testing a condition, and adding, removing or totaling.
Can I remove inside an enhanced for loop?
No. It causes an error. Use an index loop instead.
What is the difference between remove(int) and remove(Object)?
With an int argument, remove deletes the element at that index. Your code should be clear about which one you mean.
Do I need to import ArrayList?
The exam code assumes java.util is available. Write your method as the question gives it.
Where can I find the official scoring rules?
Exam formats change a little from year to year, so check the current course and exam description on the College Board site before test day.
Keep practicing
The AP Computer Science A course has eight free-response prompts, two of each exam type, each with a coding space, the scoring notes, and the canonical solution explained point by point. These free lessons also build the skills the free-response section tests:
See the AP Computer Science A course Free lessons and lesson list
Other free-response guides:
- AP Computer Science A FRQ Guide: The Four Coding Questions and the 9-Point Rubric
- AP Computer Science A Question 1 Explained: Methods and Control Structures
- AP Computer Science A Question 2 Explained: Writing a Complete Class
- AP Computer Science A Question 4 Explained: Working with a 2D Array
AP is a registered trademark of the College Board, which is not affiliated with and does not endorse this site. This guide is our own explanation of the question types and scoring. For the official scoring guidelines, see the College Board.