AP® Computer Science A FRQ Practice: Free-Response Questions with Solutions
Work through AP Computer Science A free-response 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 132
Question 133
Question 134
Question 135
Question 136
A regional airport uses a computerized scheduling system to manage daily flight departures. Each flight has a scheduled departure time and an actual departure time, both stored as integers in 24-hour format where the value represents hours and minutes combined. For example, 1430 represents 2:30 PM and 905 represents 9:05 AM. You will write two methods for the FlightScheduler class to help the operations team analyze the day's performance.
PART A
Write the method public int minutesBetween(int time1, int time2) that converts two times from HHMM format to total minutes since midnight and returns the absolute difference. For example, minutesBetween(1430, 900) should return 330 because \(14\times60+30 = 870\) minutes and \(9\times60+0 = 540\) minutes, and the absolute difference is 330.
public int minutesBetween(int time1, int time2) {
PART B
Write the method public int countDelays(int[] scheduled, int[] actual, int threshold) that returns the number of flights where the actual departure was more than threshold minutes after the scheduled time. You may call the minutesBetween method from Part A. A flight is only considered delayed if the actual time is later than the scheduled time and the difference exceeds the threshold.
For example, if scheduled is [800, 1200, 1500] and actual is [830, 1155, 1545] with threshold 20, the method returns 2 because the first flight (30 minutes late) and the third flight (45 minutes late) exceed the threshold.
public int countDelays(int[] scheduled, int[] actual, int threshold) {Main Solution
Part A Solution:
public int minutesBetween(int time1, int time2) {
int minutes1 = (time1 / 100) * 60 + (time1 % 100);
int minutes2 = (time2 / 100) * 60 + (time2 % 100);
return Math.abs(minutes1 - minutes2);
}
This method converts each HHMM time to total minutes since midnight by extracting hours (time/100) and minutes (time%100), then calculating total minutes and returning the absolute difference.
Part B Solution:
public int countDelays(int[] scheduled, int[] actual, int threshold) {
int count = 0;
for (int i = 0; i < scheduled.length; i++) {
if (actual[i] > scheduled[i]) {
int delay = minutesBetween(scheduled[i], actual[i]);
if (delay > threshold) {
count++;
}
}
}
return count;
}
This method iterates through the arrays, checks if actual time is later than scheduled time, calculates the delay using minutesBetween, and counts flights where the delay exceeds the threshold.
Teacher Explanation
Education Coach Advice
Traps
Question 137
Question 138
A small neighborhood grocery store tracks how many customers are waiting in each of its checkout lanes. The store uses the class GroceryStore, whose skeleton is shown below.
public class GroceryStore
{
/** The number of customers currently waiting in each lane.
* laneQueues[k] stores the customers in lane k (0-based).
* The array is never null and has at least one element. */
private int[] laneQueues;
/** The number of seconds it takes to serve one customer
* in any lane. Always positive. */
private int secondsPerCustomer;
/** Constructs a GroceryStore with the given queue snapshot and
* per-customer service time. */
public GroceryStore(int[] queues, int secs)
{
/* not shown */;
}
/** Returns the estimated wait time, in seconds, for a customer
* who joins the back of lane laneIndex right now. Part (a) */
public int estimatedWait(int laneIndex)
{
/* part (a) */;
}
/** Returns the index of the lane with the shortest estimated wait.
* If multiple lanes are tied, the lowest such index is returned.
* Part (b) */
public int fastestLane()
{
/* part (b) */;
}
// Other methods and constructors are not shown.
}Part A
Write the method estimatedWait. A customer joining the back of a lane must wait for every customer already in that lane to be served plus the time to be served themselves. Each customer takes exactly secondsPerCustomer seconds.
For example, suppose secondsPerCustomer is 45 and laneQueues is {3, 1, 4, 2}. The following table shows the expected return values.
| Call | Returned Value | Explanation |
|---|---|---|
estimatedWait(0) |
180 | 3 ahead + 1 self = 4 × 45 |
estimatedWait(1) |
90 | 1 ahead + 1 self = 2 × 45 |
estimatedWait(2) |
225 | 4 ahead + 1 self = 5 × 45 |
estimatedWait(3) |
135 | 2 ahead + 1 self = 3 × 45 |
Assume laneIndex is a valid index in laneQueues.
Part B
Write the method fastestLane. It returns the index of the lane with the shortest estimated wait for a new customer joining now. If two or more lanes are tied for the minimum, return the smallest such index.
For example, if secondsPerCustomer is 30 and laneQueues is {2, 0, 2, 0, 3}, then the estimated waits are {90, 30, 90, 30, 120}. The minimum is 30, achieved by lanes 1 and 3, so fastestLane() returns 1.
You must call estimatedWait appropriately to receive full credit. Assume estimatedWait works as specified regardless of what you wrote in Part A.
Main Solution
Part A — estimatedWait
The formula is: (number of customers already in the lane + 1) × secondsPerCustomer. The "+1" accounts for the joining customer themselves.
public int estimatedWait(int laneIndex)
{
return (laneQueues[laneIndex] + 1) * secondsPerCustomer;
}
Explanation:
laneQueues[laneIndex]gives the number of customers currently waiting in lanelaneIndex.- The new customer must wait for all of those customers to be served, then be served themselves — that is
laneQueues[laneIndex] + 1total customers. - Multiplying by
secondsPerCustomerconverts the customer count to seconds.
Verification with the example (secondsPerCustomer = 45, laneQueues = {3, 1, 4, 2}):
estimatedWait(0): (3+1)×45 = 180 ✓estimatedWait(1): (1+1)×45 = 90 ✓estimatedWait(2): (4+1)×45 = 225 ✓estimatedWait(3): (2+1)×45 = 135 ✓
Part B — fastestLane
Iterate over all lanes, calling estimatedWait for each. Track the index of the lane with the current minimum. Because we initialize with lane 0 and update only when strictly less than the current minimum, ties are broken in favor of the lower index automatically.
public int fastestLane()
{
int bestIndex = 0;
int bestWait = estimatedWait(0);
for (int i = 1; i < laneQueues.length; i++)
{
int wait = estimatedWait(i);
if (wait < bestWait)
{
bestWait = wait;
bestIndex = i;
}
}
return bestIndex;
}
Explanation:
- We initialize
bestIndex = 0andbestWait = estimatedWait(0)before the loop. - For each subsequent lane index
i, we computeestimatedWait(i)and compare to the running minimum. - We update only when
wait < bestWait(strictly less), so the first (lowest-indexed) minimum is retained on ties. - After the loop,
bestIndexholds the index of the lane with the shortest wait.
Verification with the example (secondsPerCustomer = 30, laneQueues = {2, 0, 2, 0, 3}):
- Waits: {90, 30, 90, 30, 120}
- Minimum is 30, first achieved at index 1 → returns 1 ✓
Teacher Explanation
Part A tests the ability to access an array element and apply a simple arithmetic formula. Students must recognize that the new customer must wait for all existing customers plus themselves, giving (laneQueues[laneIndex] + 1) * secondsPerCustomer.
Part B tests the standard "find minimum index" pattern while requiring correct use of Part A's method. Full credit requires calling estimatedWait (not recomputing the formula inline). Tie-breaking in favor of the lowest index is achieved naturally by initializing to index 0 and updating only on strict improvement.
Education Coach Advice
AP CS A FRQ Strategy:
- Read the Javadoc carefully — the comments tell you exactly what the method must do.
- Always use the provided helper methods (Part A's
estimatedWaitin Part B) — graders explicitly reward this and penalize reimplementation. - For "find minimum" problems, initialize to the first element/index before the loop, then use strict
<for updating so ties favor the lower index. - After writing your answer, mentally trace through the given examples to self-check — the problem gives you test cases for free.
- Keep methods short and focused; avoid extra variables unless they improve clarity.
Traps
Part A common errors:
- Forgetting to add 1 for the joining customer — returning
laneQueues[laneIndex] * secondsPerCustomerinstead of(laneQueues[laneIndex] + 1) * secondsPerCustomer. - Using the wrong array index (off-by-one or using the wrong variable name).
Part B common errors:
- Not calling
estimatedWait(computing the formula inline loses credit). - Updating
bestIndexon<=instead of<, which would return the last minimum index rather than the first. - Initializing
bestWaittoInteger.MAX_VALUEbut forgetting to setbestIndexproperly, or starting the loop at index 0 causing double-evaluation (minor, not penalized but inelegant). - Using
laneQueues.length - 1as the loop bound and missing the last lane.
Answer images
Question 139
An electric utility models its service area as a network of power plants and demand regions. The utility keeps three parallel data structures:
generation— adouble[]wheregeneration[p]is the rated output of plant p, in megawatts (MW).demand— adouble[]wheredemand[r]is the instantaneous load of region r, in megawatts (MW).distance— adouble[][]wheredistance[p][r]is the length of the transmission line from plant p to region r, in kilometers. A value of-1.0indicates that no line exists.
Power delivered to a region is reduced by transmission loss. The utility uses the following efficiency factor for a line of length d kilometers:
efficiency(d) = 1.0 - 0.002 * d / / d >= 0
If the efficiency is negative for some d, the method is still required to return the literal result of the formula (the utility will reject such a route elsewhere).
You will implement two methods of the EnergyGrid class. A partial class declaration is shown below.
public class EnergyGrid
{
private double[] generation;
private double[] demand;
private double[][] distance;
/ / constructor and other methods not shown
public double powerDelivered(int plant, int region)
{ / * to implement in part (a) * / }
public int closestAdequatePlant(int region)
{ / * to implement in part (b) * / }
}
Part (a) — powerDelivered
Write the method powerDelivered. The method returns the power (in MW) that plant plant delivers to region region, computed as generation[plant] * efficiency(distance[plant][region]). If no line exists between the plant and the region (distance[plant][region] equals -1.0), the method must return 0.0 without computing the formula.
Method Signature
public double powerDelivered(int plant, int region)
For example, suppose generation = {400.0, 260.0}, demand = {150.0, 300.0, 90.0}, and
distance = {
{ 50.0, 120.0, - 1.0 },
{220.0, 80.0, 30.0 }
}
Then powerDelivered(0, 0) returns 400.0 * (1.0 - 0.002 * 50.0) = 360.0. powerDelivered(0, 2) returns 0.0 because no line exists. powerDelivered(1, 1) returns 260.0 * (1.0 - 0.002 * 80.0) = 218.4.
Part (b) — closestAdequatePlant
Write the method closestAdequatePlant. The method returns the index of the plant that is closest to region region (smallest distance[plant][region]) among all plants that satisfy both of the following conditions:
- a transmission line exists between the plant and the region; that is,
distance[plant][region]is not-1.0; - the power delivered to the region by that plant, as computed in part (a), is at least
demand[region].
If no plant satisfies both conditions, the method returns -1. If several plants tie for the smallest distance, the method returns the smallest index among them.
Assume part (a) is implemented correctly. You must call powerDelivered; do not repeat its formula.
Method Signature
public int closestAdequatePlant(int region)
Continuing the example above: for closestAdequatePlant(1), plant 0 delivers 400 * (1 - 0.002 * 120) = 304.0 MW, which meets the 300 MW demand, and plant 1 delivers 260 * (1 - 0.002 * 80) = 218.4 MW, which does not. Only plant 0 qualifies, so the method returns 0. For closestAdequatePlant(2) only plant 1 has a line (distance 30, delivery 260 * 0.94 = 244.4 MW > 90), so the method returns 1.
Main Solution
Sample Solution
Part (a)
public double powerDelivered(int plant, int region)
{
if (distance[plant][region] == -1.0)
return 0.0;
double eff = 1.0 - 0.002 * distance[plant][region];
return generation[plant] * eff;
}
Part (b)
public int closestAdequatePlant(int region)
{
int closestPlant = -1;
double closestDistance = Double.MAX_VALUE;
for (int plant = 0; plant < generation.length; plant++)
{
if (distance[plant][region] != -1.0 &&
powerDelivered(plant, region) >= demand[region])
{
if (distance[plant][region] < closestDistance)
{
closestDistance = distance[plant][region];
closestPlant = plant;
}
}
}
return closestPlant;
}
Scoring. For part (a), check for the missing line (-1.0) before doing arithmetic. For part (b), iterate through all plants, call powerDelivered, require the delivered power to meet or exceed demand[region], and keep the plant with the smallest distance. Using < instead of <= preserves the smallest index in case of a tie.
Teacher Explanation
Education Coach Advice
Traps
Answer images
Question 140
A technical workshop offers several sessions throughout the day. The workshop stores information about each session in three parallel arrays.
capacity[i]is the maximum number of students allowed in sessioni.enrolled[i]is the number of students currently enrolled in sessioni.canceled[i]istrueif sessionihas been canceled andfalseotherwise.
You will write two methods of the WorkshopSchedule class. A partial class declaration is shown below.
public class WorkshopSchedule
{
private int[] capacity;
private int[] enrolled;
private boolean[] canceled;
// constructor not shown
public int openSeats(int session)
{ /* to be written in part (a) */ }
public int firstAvailableSession(int needed)
{ /* to be written in part (b) */ }
}
Part (a). Write the method openSeats, which returns the number of open seats in session session. If the session is canceled, the method returns 0.
For example, if capacity[2] = 24, enrolled[2] = 19, and canceled[2] = false, then openSeats(2) returns 5. If canceled[2] = true, then openSeats(2) returns 0.
Part (b). Write the method firstAvailableSession, which returns the index of the first session that has at least needed open seats. The method must ignore canceled sessions. If no session has at least needed open seats, the method returns -1.
Assume part (a) works as intended. Your solution to part (b) must call openSeats.
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.

