Array Trace Problems With Solutions (Exam-Level AP CSA)

Unit 4 — Exam Practice

Array Trace Problems With Solutions

Predict the output before looking at the answer. That is exactly what the exam requires you to do.

AP CSA Exam: May 15, 2026. Over half of MCQs require you to trace code. Practice here.
How to use this page: For each problem, read the code, trace through it on paper, write your predicted output, then check the solution. Do not skip the trace — that is the skill the exam tests.

Problem 1

Accumulator With Conditional

What is printed when the following code executes?

int[] vals = {3, 7, 2, 8, 5, 1};
int result = 0;
for (int i = 0; i < vals.length; i++)
{
    if (vals[i] > 4)
    {
        result += vals[i];
    }
}
System.out.println(result);
Answer: 20
Only values greater than 4 are added: 7 + 8 + 5 = 20. Values 3, 2, and 1 are skipped by the condition.

Problem 2

In-Place Modification

What does the array contain after this code executes?

int[] data = {10, 20, 30, 40, 50};
for (int i = 0; i < data.length - 1; i++)
{
    data[i] = data[i + 1];
}
// What is data now?
Answer: {20, 30, 40, 50, 50}
Each element is replaced by the one to its right. The last element (50) is never overwritten because the loop stops at length - 1. This is a left-shift pattern.

Problem 3

Swap Adjacent Pairs

What does the array contain after execution?

int[] arr = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < arr.length - 1; i += 2)
{
    int temp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = temp;
}
// What is arr now?
Answer: {2, 1, 4, 3, 6, 5}
The loop increments by 2, swapping pairs: (1,2) becomes (2,1), (3,4) becomes (4,3), (5,6) becomes (6,5).

Problem 4

Nested Condition With Count

What is printed?

int[] scores = {85, 92, 78, 95, 88, 76, 91};
int count = 0;
for (int k = 0; k < scores.length; k++)
{
    if (scores[k] >= 90)
    {
        count++;
    }
}
System.out.println(count);
Answer: 3
Values >= 90: 92, 95, 91. Three elements meet the condition.

Problem 5

Reverse Traversal With String Building

What is printed?

String[] letters = {"A", "B", "C", "D"};
String result = "";
for (int i = letters.length - 1; i >= 0; i--)
{
    result += letters[i];
}
System.out.println(result);
Answer: DCBA
The loop starts at the last index (3) and decrements to 0, concatenating each element in reverse order.

Want More Exam-Level Practice?

The 4-Week Cram Kit includes daily trace drills for every unit.

Get the Cram Kit — $29.99 1-on-1 Tutoring

Problem 6

Aliasing Trace

What is printed?

int[] x = {5, 10, 15};
int[] y = x;
y[1] = 99;
System.out.println(x[1]);
Answer: 99
y = x makes both variables point to the same array. Changing y[1] also changes x[1]. This is aliasing.

Problem 7

Find Index of Minimum

What is printed?

int[] nums = {14, 7, 23, 3, 18};
int minIdx = 0;
for (int i = 1; i < nums.length; i++)
{
    if (nums[i] < nums[minIdx])
    {
        minIdx = i;
    }
}
System.out.println(minIdx + " " + nums[minIdx]);
Answer: 3 3
The minimum value is 3 at index 3. The loop finds the index, not the value — a common exam distinction.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]