AP® Computer Science A Practice Questions
Work through AP Computer Science A practice questions unit by unit, 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.
Question 3062
Question 3063
Question 3064
Question 3065
i from 1 upward, inserting arr[i] into the sorted prefix arr[0..i-1]) on the array {8, 2, 6, 1, 4}.
What is the array after the first three iterations (i = 1, 2, 3) of the outer loop?Main Solution
Answer
Answer: B. Insertion sort builds a sorted prefix one element at a time. Starting from{8, 2, 6, 1, 4}:i = 1: insert
2 into {8} → {2, 8, 6, 1, 4}.i = 2: insert
6 into {2, 8} → {2, 6, 8, 1, 4}.i = 3: insert
1 into {2, 6, 8} → {1, 2, 6, 8, 4}.Option A stops after two iterations; option C is the final fully sorted result after all iterations.
Answer: Correct: B
A
{2, 6, 8, 1, 4}
B
{1, 2, 6, 8, 4}
C
{1, 2, 4, 6, 8}
D
{2, 1, 6, 8, 4}
Answer images
Question 3066
Question 3067
Question 3068
Question 3069
Question 3070
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
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.

