Lesson 18: FRQ Practice Set 1 — Methods and Control StructuresAP
🎧 Listen to this lesson
This practice set trains FRQ type 1 — Methods and Control Structures — the question that asks you to write a self-contained method from a tight specification, using loops and conditionals. It is the most mechanical of the four FRQs and, with the Lesson 15 protocol, the most reliably scored. Below are two fully worked problems in exam style — study the *approach notes* as much as the solutions — followed by fresh exam questions.
Worked Problem A: longest streak
Prompt. Write the static method longestStreak, which takes an int array of daily temperatures and an int threshold, and returns the length of the longest run of consecutive days with temperature greater than or equal to the threshold. If no day meets the threshold, return 0. Example: for {70, 82, 85, 60, 90, 91, 92, 40} with threshold 80, the runs are (82, 85) of length 2 and (90, 91, 92) of length 3, so the method returns 3. Approach notes. This is a "current run + best run" algorithm — a pattern worth naming, because it recurs constantly. State: current (length of the run ending at the current position) and best (the maximum current ever seen). On a qualifying element, current++ and possibly update best; on a non-qualifying element, current resets to 0. The reset is the soul of the problem — students who forget it compute "total qualifying days" instead and earn partial credit at best. Model solution.
public static int longestStreak(int[] temps, int threshold)
{
int best = 0;
int current = 0;
for (int i = 0; i < temps.length; i++)
{
if (temps[i] >= threshold)
{
current++;
if (current > best)
{
best = current;
}
}
else
{
current = 0;
}
}
return best;
}
Self-scoring checklist. Header exact? (1) Loop visits every element? (1) Qualifying test uses >= (the prompt said "greater than or equal to")? (1) Run increments and best updates inside the qualifying branch? (2) Run resets on a non-qualifying element? (2) Returns best, with 0 correct when nothing qualifies? (2)
Trace check on the example: runs build 0 → (70 no) → 1, 2 → reset → 1, 2, 3 → reset; best ends at 3. ✓
Worked Problem B: alternating sum
Prompt. Write the static method alternatingSum, which takes an int array and returns the alternating sum of its elements: the element at index 0 is added, index 1 subtracted, index 2 added, and so on. For {10, 3, 5, 2} the method returns 10 − 3 + 5 − 2 = 10. For an empty array it returns 0. Approach notes. Index parity decides the sign: even index → add, odd index → subtract, tested with i % 2 == 0 (Unit 1's remainder earns its keep). The alternative — a boolean add flag flipped each iteration — is equally correct; the rubric pays for behaviour. Watch the empty-array edge: a loop that never runs leaves the accumulator at 0, which is exactly the required answer — no special case needed, *because* the initialisation was right. Model solution.
public static int alternatingSum(int[] arr)
{
int result = 0;
for (int i = 0; i < arr.length; i++)
{
if (i % 2 == 0)
{
result += arr[i];
}
else
{
result -= arr[i];
}
}
return result;
}
Self-scoring checklist. Header exact? (1) Every element visited once? (1) Sign decided by index parity (or an equivalent alternating state)? (2) Even indices added, odd subtracted — not the reverse? (2) Accumulator starts at 0 so the empty array returns 0? (1) Returns the result? (2)
The pattern library you are building
Notice what these two problems share with the rest of the course: an accumulator, a state variable carried across iterations (run length, sign, best-so-far), a boundary-sensitive comparison, and an edge case decided by initialisation. Type 1 FRQs are variations on this theme with different costumes. When a new prompt appears, your first move is not to code — it is to name the state variables. If you can say "I need a current-X and a best-X" or "I need a sum and a sign rule", the method writes itself.
Common mistakes specific to this question type
- Returning inside the loop unconditionally — the method exits on the first element instead of after the scan (search-style early returns are the exception, and only on success).
- Updating the best-so-far in the wrong branch (e.g. only when a run *ends* — which misses a run that continues to the array's final element; the worked solution updates continuously to dodge exactly that).
- Reading "greater than or equal" as "greater than" — prompts choose their comparators deliberately; underline them (Lesson 15's protocol).
- Special-casing the empty array with a crash:
arr[0]before checking length. Initialise so the general code handles the edge.
longestStreak mentally for the prompt variant "return the starting index of the longest run". (Track bestStart alongside best, updating it to i - current + 1 whenever a new best is set.)AP Exam Experience
Question 1. Consider the following code segment.
int count = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i == j)
{
count++;
}
}
}
System.out.println(count);
What is printed?
- (A)
0 - (B)
4 - (C)
8 - (D)
16 - (E)
2
Answer: (B). The inner body executes 16 times, but the increment fires only when i == j — true for exactly one j per row, 4 times in total. (D) is the "count the executions" misread; the condition is the question.
Question 2. Consider the following code segment.
String s = "banana";
int count = 0;
for (int i = 0; i < s.length(); i++)
{
if (s.substring(i, i + 1).equals("a"))
{
count++;
}
}
System.out.println(count);
What is printed?
- (A)
1 - (B)
2 - (C)
3 - (D)
4 - (E) Nothing; an exception is thrown.
Answer: (C). "banana" contains three a's (indices 1, 3, 5). The one-character substring at the final index is substring(5, 6) — legal, because the end index may equal the length. (E) tempts students who think the last character is unreachable.
Question 3. Consider the following method.
public static int clamp(int value, int max)
{
if (value > max)
{
return max;
}
else if (value < 0)
{
return 0;
}
return value;
}
What is returned by clamp(-3, 10)?
- (A)
-3 - (B)
0 - (C)
10 - (D)
7 - (E) Nothing; the method does not compile.
Answer: (B). The first condition fails (−3 is not > 10); the else-if catches negatives and returns 0. The final return value is reached only for in-range values — a three-path method, and each path must be checked separately.
Free-Response Question. Write the static method longestFlat, which takes an int array and returns the length of the longest run of consecutive equal values in the array. For {5, 5, 2, 2, 2, 7} the runs are (5, 5) of length 2, (2, 2, 2) of length 3, and (7) of length 1, so the method returns 3. For an array with one element, the method returns 1. Assume the array has at least one element.
Scoring guideline (9 points).
- +1 Correct method header: static, returns
int, oneint[]parameter - +1 Initialises both the current run length and the best run length consistently with a single element (both effectively start at 1)
- +1 Traverses adjacent pairs (compares each element with its predecessor or successor within valid bounds)
- +1 Increments the current run when adjacent elements are equal
- +1 Resets the current run to 1 when adjacent elements differ
- +1 Updates the best run whenever the current run exceeds it — including a run that ends at the final element
- +1 Returns the best run length
- +1 A single-element array returns 1 (no crash, no special-case error)
- +1 No index ever goes out of bounds during traversal
Model solution.
public static int longestFlat(int[] arr)
{
int best = 1;
int current = 1;
for (int i = 1; i < arr.length; i++)
{
if (arr[i] == arr[i - 1])
{
current++;
}
else
{
current = 1;
}
if (current > best)
{
best = current;
}
}
return best;
}
## Worked Problem C: balanced halves
**Prompt.** Write the static method `isBalanced`, which takes an `int` array with an **even** number of elements and returns `true` if the sum of the elements in the first half equals the sum of the elements in the second half, and `false` otherwise. For {1, 2, 3, 0, 4, 2} the first half sums to 6 and the second half to 6, so the method returns `true`. For {5, 1, 2, 2} it returns `false` (6 versus 4).
**Approach notes.** The midpoint is `arr.length / 2` — integer division doing honest work for once. Two accumulators (or one accumulator and a comparison loop) both work; the clean structure computes both sums in one pass by branching on whether the index is below the midpoint, or in two short loops. The two-loop version is worth preferring under exam pressure: each loop is a trivially checkable sum, and the boundary between them is a single value to verify.
**Model solution.**
public static boolean isBalanced(int[] arr) { int mid = arr.length / 2; int first = 0; int second = 0; for (int i = 0; i < mid; i++) { first += arr[i]; } for (int i = mid; i < arr.length; i++) { second += arr[i]; } return first == second; }
**Self-scoring checklist.** Header exact (returns `boolean`)? (1) Midpoint computed as `length / 2`? (1) First loop covers exactly indices 0..mid−1? (2) Second loop covers exactly mid..length−1, with no gap or overlap at the boundary? (2) Both sums accumulated (not overwritten)? (1) Returns the comparison of the two sums (not a sum, not a count)? (2)
**Variant drill.** Change one word of the prompt — "returns the **number of runs** of equal values" instead of the longest run's length — and the same skeleton solves it: count a new run whenever an element differs from its predecessor (plus one for the first element). For {5, 5, 2, 2, 2, 7} the answer is 3 runs. Training yourself to re-derive a method under a one-word prompt change is the highest-yield FRQ practice there is, because the real exam's questions are precisely last year's skeletons with one word changed.
📊 Interactive
Interactive — hover or explore the chart above.
🎬 Watch & Learn
Free YouTube video lessons on “FRQ Practice Set 1 — Methods and Control Structures” — pick a result below:
- AP Computer Science A — FRQ Practice Set 1 — Methods and Control Structures: full video lesson
- FRQ Practice Set 1 — Methods and Control Structures on freeCodeCamp
- FRQ Practice Set 1 — Methods and Control Structures on CS Dojo
- FRQ Practice Set 1 — Methods and Control Structures on Khan Academy
- Exam practice & worked examples: FRQ Practice Set 1 — Methods and Control Structures