AP CSA Unit-Level MCQ
Array Traversal
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What array traversal covers
Standard traversal runs an index from 0 while it is less than length, visiting every element once. The enhanced for loop gives read-only access to each element without an index and is the cleaner choice when position does not matter. Choosing between them is a real decision: you need the indexed form whenever you must write to the array, compare neighbors, or know where a value sits.
Where students lose points
Using <= length throws an ArrayIndexOutOfBoundsException, and starting at 1 silently skips the first element. Students also reach for the enhanced for loop when the task requires modification, then cannot explain why the array is unchanged afterward.
How it shows up on the AP exam
Unit 4 material and the backbone of array FRQs. Almost every array question begins with a traversal, so a wrong bound costs points before the interesting logic even starts.
For practice use only.
Array Traversal MCQ Practice
AP CSA array traversal practice: indexed versus enhanced for loops, summing and counting elements, and reading exactly the right range of indexes.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Array Traversal: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1};
for (int v : data) {
v = v * 2;
}
int sum = 0;
for (int v : data) sum += v;
System.out.print(sum);All 15 array traversal questions
Work through the interactive quiz above first. This is the full array traversal question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Array Traversal: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1}; for (int v : data) { v = v * 2; } int sum = 0; for (int v : data) sum += v; System.out.print(sum);- 28
- 56
- 8
- 0
Show worked solution
Correct answer: 28
The enhanced for loop copies each element into v, so assigning to v never reaches the array itself.
2.Code Tracing 12 - Array Traversal: follow the variable updates.
int[] data = {6, 7, 4, 9, 7, 3}; String out = ""; for (int i = data.length - 1; i >= 0; i--) { out += data[i] + "-"; } System.out.print(out);- 6-7-4-9-7-3
- 3-7-9-4-7-6-
- 6-7-4-9-7-3-
- 3-7-9-4-7-6
Show worked solution
Correct answer: 3-7-9-4-7-6-
Counting down from length() - 1 walks the array backwards, and the separator is appended after every element including the last.
3.Code Tracing 28 - Array Traversal: trace the branch and loop path.
int[] data = {9, 7, 2, 8, 10, 6}; int sum = 0; for (int i = 1; i < data.length - 1; i++) { sum += data[i]; } System.out.print(sum);- 33
- 27
- 42
- 36
Show worked solution
Correct answer: 27
Starting at 1 skips the first element and stopping before length - 1 skips the last.
4.Code Tracing 39 - Array Traversal: watch the index changes.
int[] data = {11, 8, 4, 10, 6, 8}; int count = 0; for (int i = 0; i < data.length - 1; i++) { if (data[i] < data[i + 1]) count++; } System.out.print(count);- 5
- 6
- 2
- 3
Show worked solution
Correct answer: 2
Comparing adjacent pairs requires stopping one index early so data[i + 1] stays in bounds.
5.Code Tracing 50 - Array Traversal: evaluate the state change step by step.
int[] data = {13, 9, 6, 8, 8, 10}; int sum = 0; for (int i = 0; i <= data.length; i++) { sum += data[i]; } System.out.print(sum);- 54
- 6
- (no output)
- A runtime exception is thrown.
Show worked solution
Correct answer: A runtime exception is thrown.
Using <= with length runs one pass too many and reads index length, which throws ArrayIndexOutOfBoundsException.
6.What is printed by the following code segment?
int[] a = {4, 9, 15, 40, 23, 90}; int c = 0; for (int i = 1; i < a.length; i++) { if (a[i] > 2 * a[i - 1]) { c++; } } System.out.print(c);- 3
- 2
- 1
- 5
Show worked solution
Correct answer: 3
The jumps that more than double are 4 to 9, 15 to 40, and 23 to 90. A strict comparison excludes any pair that exactly doubles.
7.What is printed by the following code segment?
int[] a = {1, 2, 3, 4, 5}; int s = 0; for (int i = 0; i < a.length; i++) { s += i % 2 == 0 ? a[i] : -a[i]; } System.out.print(s);- 15
- 3
- -3
- 9
Show worked solution
Correct answer: 3
The signs alternate by index, giving 1 - 2 + 3 - 4 + 5.
8.What is printed by the following code segment?
int[] a = {7, 3, 9, 3, 1}; int idx = 0; for (int i = 1; i < a.length; i++) { if (a[i] <= a[idx]) { idx = i; } } System.out.print(idx);- 1
- 3
- 4
- 0
Show worked solution
Correct answer: 4
Using less than or equal keeps moving on ties, so the last smallest position wins.
9.What is printed by the following code segment?
int[] a = {5, 5, 5, 2, 5}; boolean all = true; boolean any = false; for (int v : a) { if (v != 5) { all = false; } if (v == 2) { any = true; } } System.out.print(all + " " + any);- true true
- false false
- true false
- false true
Show worked solution
Correct answer: false true
One element breaks the universal test and the same element satisfies the existence test.
10.What is printed by the following code segment?
int[] a = {3, 6, 9, 12}; int c = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[j] % a[i] == 0) { c++; } } } System.out.print(c);- 4
- 6
- 2
- 3
Show worked solution
Correct answer: 4
The qualifying pairs are 3 with 6, 3 with 9, 3 with 12, and 6 with 12. Neither 6 nor 9 divides 9 or 12 otherwise.
11.What is printed by the following code segment?
int[] a = {1, 3, 5, 7}; int t = 0; for (int i = 0; i < a.length / 2; i++) { t += a[i] * a[a.length - 1 - i]; } System.out.print(t);- 16
- 22
- 7
- 15
Show worked solution
Correct answer: 22
The mirrored pairs are 1 with 7 and 3 with 5, giving 7 plus 15.
12.What is printed by the following code segment?
int[] a = {2, 2, 3, 3, 3, 4}; int runs = 1; for (int i = 1; i < a.length; i++) { if (a[i] != a[i - 1]) { runs++; } } System.out.print(runs);- 6
- 2
- 3
- 4
Show worked solution
Correct answer: 3
Each change of value starts a new run, and there are two changes.
13.What is printed by the following code segment?
int[] a = {10, 20, 30}; int s = 0; for (int i = 0; i <= a.length - 1; i++) { s += a[i]; } System.out.print(s);- 30
- An exception is thrown.
- 50
- 60
Show worked solution
Correct answer: 60
Using less than or equal with length minus one is equivalent to the usual bound and is safe.
14.What is printed by the following code segment?
int[] a = {4, 1, 8, 3}; int max = 0; for (int v : a) { if (v > max) { max = v; } } int[] b = {-4, -1, -8}; int max2 = 0; for (int v : b) { if (v > max2) { max2 = v; } } System.out.print(max + " " + max2);- 8 0
- 8 -1
- 4 -1
- 8 -8
Show worked solution
Correct answer: 8 0
Seeding from 0 works for the positive array but wrongly returns 0 for the negative one, which is why seeding from an element is safer.
15.What is printed by the following code segment?
int[] a = {1, 2, 3, 4}; int f = 0; for (int i = 0; i < a.length; i++) { if (a[i] % 2 == 0) { f = a[i]; i = a.length; } } System.out.print(f);- 4
- 2
- 6
- 0
Show worked solution
Correct answer: 2
Setting the index past the bound ends the search at the first even value.
