Once you know how to create an array, the next step is learning how to process the data inside it. In AP Computer Science A, array traversal simply means using a loop to visit every element in an array, one by one.
In this interactive practice set, we cover Topic 6.2: Traversing Arrays. These 10 multiple-choice questions will test your ability to trace for loops, while loops, and identify the most common coding errors students make on the AP exam.
Key Concepts to Remember Before You Start:
- The Bounds Limit: A standard
forloop to traverse an entire array should start ati = 0and end with the conditioni < array.length. - Off-By-One Errors: Using
i <= array.lengthis the most common mistake in Java. Because arrays are zero-indexed, the last element is atlength - 1. Trying to access the exactlengthindex will throw anArrayIndexOutOfBoundsException. - Index vs. Element: Always pay attention to whether the code is modifying the loop counter variable (
i) or the actual data inside the array (arr[i]). - Partial Traversal: You don’t always have to visit every element. Loops can step by twos (
i += 2) or go backward (i--).
Grab a piece of scratch paper to trace the variable values, and test your logic with the quiz below!
Topic 6.2 Practice
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.print(sum);
sum variable: 2 + 4 + 6 + 8 = 20.for (int i = 0; i <= vals.length; i++) {
System.out.print(vals[i] + ” “);
}
i <= vals.length, the loop will run when i is 3. It successfully prints 10, 20, and 30, but crashes when attempting to access vals[3].for (int i = arr.length – 1; i > 0; i–) {
System.out.print(arr[i]);
}
arr.length - 1, which is 4). However, look closely at the loop condition: i > 0. The loop will stop before it reaches index 0. Therefore, it prints elements at index 4, 3, 2, and 1, but skips the element at index 0 (which is 1).for (int i = 1; i < nums.length; i += 2) {
System.out.print(nums[i] + ” “);
}
i += 2, which means the index jumps by 2 on each iteration. The loop processes index 1, then index 3 (value 40). When i becomes 5, the loop terminates.for (int i = 0; i < data.length – 1; i++) {
data[i] = data[i + 1];
}
System.out.print(data[3]);
data[0] becomes 2, data[1] becomes 3, and data[2] becomes 4. The loop stops when i is 2 because of the condition i < data.length - 1 (which is i < 3). Index 3 is never modified! It remains 4. The final array is {2, 3, 4, 4}.names and prints every element using a while loop?i to 0, checks against the array length property (without parentheses), and increments i AFTER printing. Option A uses length(), which is for Strings, not arrays. Option D increments i before printing, which will skip index 0 and eventually throw an OutOfBounds error.int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 10 == 0) {
count++;
}
}
System.out.println(count);
count variable increments 3 times.for (int i = 0; i < myArr.length; i++) {
myArr[i] = myArr[i] * 2;
}
System.out.print(myArr[2]);
i is initialized to 0, but it is never incremented inside the body of the while loop. Because i will always be 0 (and always less than arr.length), the condition will never become false, causing an infinite loop.int max = vals[0];
for (int i = 1; i < vals.length; i++) {
if (vals[i] > max) {
max = vals[i];
}
}
System.out.println(max);
max to the first element (5), then starts the loop at index 1. It compares each subsequent element to max, updating max whenever it finds a larger number. The loop finds that 8 > 5, updates max, and ignores 3. The final result printed is 8.Unit 6.2 Practice Complete!
The Secret to Array Traversal: Paper Tracing
If you found yourself guessing on Questions 4, 5, or 10, you are falling into a common trap: trying to run the code in your head.
The human brain is excellent at many things, but it is terrible at remembering the exact state of variables jumping around inside a looping algorithm. When you sit for the AP Computer Science A exam, the College Board intentionally writes questions that are too complex to track mentally.
The Solution: Always build a trace table. Write down the loop variable (i) in one column, and the array elements (arr[i]) in another. Cross out the old values and write the new ones as you read through the loop line by line.
Ready for the Next Step?
Standard for loops are powerful, but sometimes they require a lot of typing. Java offers a shortcut designed specifically for reading data out of arrays quickly and safely, without ever having to worry about an ArrayIndexOutOfBoundsException.
Continue your AP CSA review by moving on to Topic 6.3: Enhanced For-Loop Practice, where we break down the syntax, advantages, and hidden limitations of the "for-each" loop.