FRQ
› csapa
DiverseArray: 2015 FRQ 1
A step-by-step solution to the 2015 AP CSA FRQ 1 (DiverseArray), covering summing a one-dimensional array, computing two-dimensional array row sums, and detecting duplicate sums in Java.
Three related methods about summing and comparing arrays make up this AP Computer Science A free-response question — starting with a single one-dimensional sum, then building up to comparing sums across every row of a two-dimensional array.
What This FRQ Tests
- AP CSA units: Unit 6 (Array), Unit 8 (2D Array), and Unit 4 (Iteration)
- Core skill: looping over a one-dimensional array to accumulate a total, then reusing that same logic across every row of a two-dimensional array
- Secondary skill: comparing values pairwise across a collection to detect duplicates
- Official category: this year's FRQ 1 is entirely array-based rather than "Methods and Control Structures." Pre-2019 AP CSA exams didn't follow today's fixed FRQ-number-to-category order (that pattern wasn't standardized until the 2019-2020 Course and Exam Description redesign), so this one is best described as testing Array (Unit 6) and 2D Array (Unit 8) content directly, regardless of which numbered slot it happens to occupy.
The Setup
- A
DiverseArrayclass (not shown) holds threestaticmethods, all working together arraySum(int[] arr)— returns the sum of every entry in a one-dimensional arrayrowSums(int[][] arr2D)— returns a one-dimensional array holding the sum of each row of a two-dimensional array;arr2Dis in row-major order, soarr2D[r][c]is the entry at rowr, columncisDiverse(int[][] arr2D)— returns whether every row sum in a two-dimensional array is unique- Each part explicitly builds on the one before it:
rowSumsis required to callarraySum, andisDiverseis required to callrowSums, for full credit
Part (a): Writing arraySum(int[] arr)
Step-by-Step Approach
- Start a running total at
0. - Loop over every index of
arr. - Add each entry to the running total.
- Return the total once the loop finishes.
The Code
public static int arraySum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum += arr[i];
}
return sum;
}
Why Each Piece Matters
sumstarts at0before the loop runs — a running total that begins at the wrong value throws off every entry added afterward.arr.lengthdrives the loop bound instead of a hardcoded number, so the method works correctly no matter how largearris.sum += arr[i]is shorthand forsum = sum + arr[i]— identical behavior, just less to type.
Tracing the Example
Using the question's own array, arr1 = {1, 3, 2, 7, 3}:
| Index | Value | Running total |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 3 | 4 |
| 2 | 2 | 6 |
| 3 | 7 | 13 |
| 4 | 3 | 16 |
Final returned value: 16 — matches arraySum(arr1) exactly as given.
Common Mistakes to Avoid
- Starting
sumat1instead of0. A seemingly small typo that throws off every result by exactly 1. - Using
<=instead of<in the loop condition.i <= arr.lengthaccessesarr[arr.length], which is out of bounds and throws anArrayIndexOutOfBoundsException. - Forgetting the
returnstatement, or returningsumfrom inside the loop after only the first iteration.
Part (b): Writing rowSums(int[][] arr2D)
Step-by-Step Approach
- Create a new
int[]sized toarr2D.length— one slot per row. - Loop over every row index
r. - Each row,
arr2D[r], is itself a complete one-dimensional array — hand it directly toarraySum. - Store the value
arraySumreturns at indexrof the result array. - Return the filled array once every row has been processed.
The Code
public static int[] rowSums(int[][] arr2D)
{
int[] sums = new int[arr2D.length];
for (int r = 0; r < arr2D.length; r++)
{
sums[r] = arraySum(arr2D[r]);
}
return sums;
}
Why Each Piece Matters
arr2D[r]by itself is a full one-dimensional array — exactly whatarraySumexpects as its parameter. No separate inner loop is needed here at all.- Reusing
arraySumisn't just a style choice — the question explicitly states "you must usearraySumappropriately to receive full credit." Writing a fresh nested loop that recomputes the same sums would not earn full marks even if the output were correct. sums[r]andarr2D[r]use the same index variabler— the sum computed from rowrof the input always lands at positionrof the output.
Tracing the Example
Using the question's own mat1:
| Row | Entries | arraySum result |
|---|---|---|
| 0 | 1, 3, 2, 7, 3 | 16 |
| 1 | 10, 10, 4, 6, 2 | 32 |
| 2 | 5, 3, 5, 9, 6 | 28 |
| 3 | 7, 6, 4, 2, 1 | 20 |
Final returned array: {16, 32, 28, 20} — matches rowSums(mat1) exactly as given.
Common Mistakes to Avoid
- Writing a brand-new nested loop instead of calling
arraySum. Even if the sums come out right, this loses the credit tied to reusing the given method. - Sizing the result array by column count instead of row count —
rowSumsreturns one entry per row, so the result array's length should matcharr2D.length, not the length of an individual row. - Using a different index for the result than for the row being summed —
sums[r]must always correspond toarr2D[r], notarr2D[r - 1]orarr2D[r + 1].
Part (c): Writing isDiverse(int[][] arr2D)
Step-by-Step Approach
- Get every row's sum at once by calling
rowSums(arr2D). - Compare every pair of sums to each other.
- If any two match, the array isn't diverse — return
falseimmediately. - If no pair ever matches after checking everything, return
true.
The Code
public static boolean isDiverse(int[][] arr2D)
{
int[] sums = rowSums(arr2D);
for (int i = 0; i < sums.length; i++)
{
for (int j = i + 1; j < sums.length; j++)
{
if (sums[i] == sums[j])
{
return false;
}
}
}
return true;
}
Why Each Piece Matters
- Calling
rowSums(arr2D)first is required by the question ("you must userowSumsappropriately"), and it also means the sums only ever get computed once, not recomputed for every comparison. - The inner loop starts at
j = i + 1, not0. Starting at0would comparesums[i]to itself (always equal, which would incorrectly report every array as non-diverse) and would also re-check every pair a second time in the opposite order. - Returning
falseimmediately the moment a match is found avoids unnecessary comparisons once the answer is already known.
Tracing the Example
Using the question's own two arrays:
mat1has row sums{16, 32, 28, 20}— every pair is checked (16vs32,16vs28,16vs20,32vs28,32vs20,28vs20) and none match, so the loop finishes andisDiverse(mat1)returnstrue.mat2has row sums{14, 35, 36, 14}— the very first row (index 0, sum14) and the last row (index 3, sum14) match, soisDiverse(mat2)returnsfalseas soon as that pair is reached.
Both results match the question exactly.
Common Mistakes to Avoid
- Recomputing sums manually instead of calling
rowSums— again, the question specifically requires using the given method. - Starting the inner loop at
j = 0instead ofj = i + 1, which compares every sum to itself and breaks the whole method. - Returning
truefrom inside the loop as soon as one comparison doesn't match, instead of only returningtrueafter every pair has been checked with no matches found.
Key Takeaways
- When a problem hands you multiple related methods and says to "use" an earlier one, that's not just a suggestion — it's graded, and reimplementing the same logic instead of calling the given method can cost credit even when the output is correct.
- A row of a two-dimensional array,
arr2D[r], is itself a complete one-dimensional array — passing it directly into a method built for one-dimensional arrays is often the cleanest way to share code across dimensions. - Detecting duplicates in a list of values is a classic double-loop pattern: compare every element to every element that comes after it (starting the inner loop at
i + 1, not0) so no pair is skipped or double-checked.