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 212
Question 213
Question 214
A retail system models each item for sale as a Product and uses a Store object to hold every product currently tracked by the store. The two classes are shown below; only the method totalValueOfLowStock in Store is left for you to complete. Everything else is already written.
public class Product
{
private String name;
private double price;
private int quantity;
public Product(String theName, double thePrice, int theQuantity)
{
name = theName;
price = thePrice;
quantity = theQuantity;
}
public String getName() { return name; }
public double getPrice() { return price; }
public int getQuantity() { return quantity; }
}
public class Store
{
/** Inventory of products currently tracked by this store. Never null. */
private ArrayList<Product> inventory;
public Store(ArrayList<Product> startingInventory)
{
inventory = startingInventory;
}
/** Returns the total dollar value of all products in inventory whose current
* quantity is strictly less than threshold. The dollar value of a single
* product is its price multiplied by its quantity. If no product has a
* quantity below threshold, the method returns 0.0.
* @param threshold the cutoff quantity; assumed to be non-negative
* @return the sum of price * quantity over all low-stock products
*/
public double totalValueOfLowStock(int threshold) { /* to implement */ }
}
Write the method totalValueOfLowStock. The method must iterate over every product in inventory and accumulate the dollar value price * quantity for each product whose current quantity is strictly less than threshold.
Suppose inventory holds three products:
{name:"widget", price:4.50, quantity:2}, {name:"gizmo", price:10.00, quantity:7}, {name:"bolt", price:0.25, quantity:3}.
A call to totalValueOfLowStock(5) selects the "widget" (quantity 2) and the "bolt" (quantity 3) because 7 is not less than 5. The returned value is \(4.50 \times 2 + 0.25 \times 3 = 9.75\).
public double totalValueOfLowStock(int threshold)Main Solution
Sample Solution
public double totalValueOfLowStock(int threshold)
{
double total = 0.0;
for (int i = 0; i < inventory.size(); i++)
{
Product p = inventory.get(i);
if (p.getQuantity() < threshold)
{
total = total + p.getPrice() * p.getQuantity();
}
}
return total;
}
Scoring notes. Points are earned for: iterating over every index of inventory (a for or enhanced-for loop both work); retrieving each Product via get(i) and using its accessor methods rather than touching the private fields; comparing getQuantity() strictly to threshold; accumulating getPrice() * getQuantity() only for low-stock products; and returning the correct double total. Common errors include using a non-strict comparison (which includes products whose quantity equals threshold) or accessing inventory.get(i).price directly.
Answer images
Question 215
Question 216
A cinema management system models each scheduled showing as a Movie object and keeps every showing currently on the schedule inside a Theater object. The two classes are shown below; only the method averageRatingAtLeast in Theater is left for you to complete. Everything else is already written.
public class Movie
{
private String title;
private int lengthMinutes;
private double rating;
public Movie(String theTitle, int theLength, double theRating)
{
title = theTitle;
lengthMinutes = theLength;
rating = theRating;
}
public String getTitle() { return title; }
public int getLengthMinutes() { return lengthMinutes; }
public double getRating() { return rating; }
}
public class Theater
{
/** All movies currently scheduled at this theater. Never null. */
private ArrayList<Movie> showings;
public Theater(ArrayList<Movie> startingShowings)
{
showings = startingShowings;
}
/** Returns the average rating of every movie in showings whose length is at least
* minLength minutes. If no movie meets the length requirement, returns 0.0.
* @param minLength the inclusive lower bound on length, in minutes; assumed non-negative
* @return the mean rating of qualifying movies, or 0.0 when no movie qualifies
*/
public double averageRatingAtLeast(int minLength) { /* to implement */ }
}
Write the method averageRatingAtLeast. The method must iterate over every Movie in showings, accumulate the rating of each movie whose length in minutes is at least minLength, and return the mean of those ratings as a double. If no movie meets the length requirement, the method returns 0.0.
Suppose showings holds four movies:
{title:"Alpha", lengthMinutes:90, rating:4.0}, {title:"Bravo", lengthMinutes:135, rating:4.5}, {title:"Charlie", lengthMinutes:75, rating:3.0}, {title:"Delta", lengthMinutes:120, rating:3.5}.
A call to averageRatingAtLeast(120) selects the movies whose length is at least 120 minutes — Bravo and Delta — and returns the mean of their ratings: (4.5 + 3.5) / 2 = 4.0.
A call to averageRatingAtLeast(200) finds no qualifying movie and therefore returns 0.0.
public double averageRatingAtLeast(int minLength)Main Solution
Sample Solution
public double averageRatingAtLeast(int minLength)
{
double sum = 0.0;
int count = 0;
for (int i = 0; i < showings.size(); i++)
{
Movie m = showings.get(i);
if (m.getLengthMinutes() >= minLength)
{
sum = sum + m.getRating();
count = count + 1;
}
}
if (count == 0)
{
return 0.0;
}
return sum / count;
}
Scoring notes. Points are earned for: iterating over every index in showings (an enhanced-for loop also works); fetching each Movie through get(i) and using its accessor methods rather than touching the private fields; comparing getLengthMinutes() to minLength with >= (not >); accumulating both a running rating sum and a running qualifying count; guarding the final division with a count == 0 check that returns 0.0; and returning sum / count as a double in the normal case (legal because sum is already a double). Common errors include using >, dividing by showings.size() instead of count, and accessing showings.get(i).rating directly.
Answer images
Question 217
Question 218
Question 219
Information about applicants for a scholarship program is stored in Applicant objects.
public class Applicant
{
private String name;
private int score;
private boolean complete;
public Applicant(String n, int s, boolean c)
{
name = n;
score = s;
complete = c;
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
public boolean isComplete()
{
return complete;
}
}
The ScholarshipGroup class stores a list of applicants.
public class ScholarshipGroup
{
private ArrayList<Applicant> applicants;
public ScholarshipGroup(ArrayList<Applicant> list)
{
applicants = list;
}
public ArrayList<String> finalists(int minScore)
{ /* to be written */ }
}
Write the method finalists, which returns a new ArrayList<String> containing the names of all applicants who satisfy both of the following conditions.
- The applicant's form is complete.
- The applicant's score is at least
minScore.
Among the applicants who satisfy those conditions, only applicants whose score is equal to the highest qualifying score should appear in the returned list. Names in the returned list must appear in the same order as the corresponding applicants appear in applicants. If no applicant qualifies, the method returns an empty list.
For example, if the qualifying applicants have scores 81, 94, 94, 88, the returned list should contain the names of the two applicants whose score is 94, in their original order.
Answer images
Question 220
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(2);
values.add(5);
values.add(1);
values.add(5);
for (int i = values.size() - 1; i >= 0; i--)
{
if (values.get(i) == 5)
values.remove(i);
}
System.out.println(values);
Main Solution
Answer
Answer: D. The loop moves backward through the list, removing both occurrences of 5 without skipping any elements. The remaining values are 2 and 1.
Answer: Correct: D
A
[2, 5, 1]
B
[2, 1, 5]
C
[2]
D
[2, 1]
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.

