• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Arrays
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

AP CSA Unit 6 Practice: Array Traversal MCQs

Home » AP CSA MCQ » Unit 6

AP CSA Practice: Array Traversal

📚 Topic 6.2 ⏱️ 15 Min Practice ✅ Updated for 2027 Exam

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 for loop to traverse an entire array should start at i = 0 and end with the condition i < array.length.
  • Off-By-One Errors: Using i <= array.length is the most common mistake in Java. Because arrays are zero-indexed, the last element is at length - 1. Trying to access the exact length index will throw an ArrayIndexOutOfBoundsException.
  • 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

Question 1 of 10 | Score: 0
1. Consider the following code segment:
int[] arr = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
    sum += arr[i];
}
System.out.print(sum);
What is printed as a result of executing the code segment?
Correct Answer: B This is a standard accumulator loop. It traverses every element in the array perfectly from index 0 to 3. It adds each element to the sum variable: 2 + 4 + 6 + 8 = 20.
2. Consider the following code segment:
int[] vals = {10, 20, 30};
for (int i = 0; i <= vals.length; i++) {
    System.out.print(vals[i] + ” “);
}
What is the result of executing this code segment?
Correct Answer: C This is the classic “off-by-one” error! The array has a length of 3, so valid indices are 0, 1, and 2. Because the loop condition is 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].
3. Consider the following code segment:
int[] arr = {1, 3, 5, 7, 9};
for (int i = arr.length – 1; i > 0; i–) {
    System.out.print(arr[i]);
}
What is printed as a result of executing the code segment?
Correct Answer: B This loop iterates backward starting from the last valid index (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).
4. Consider the following code segment:
int[] nums = {10, 20, 30, 40, 50};
for (int i = 1; i < nums.length; i += 2) {
    System.out.print(nums[i] + ” “);
}
What is printed as a result of executing the code segment?
Correct Answer: B The loop does not start at 0; it starts at index 1 (which holds the value 20). The update statement is 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.
5. Consider the following code segment:
int[] data = {1, 2, 3, 4};
for (int i = 0; i < data.length – 1; i++) {
    data[i] = data[i + 1];
}
System.out.print(data[3]);
What is printed as a result of executing the code segment?
Correct Answer: B This loop shifts elements to the left. 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}.
6. Which of the following correctly traverses a String array named names and prints every element using a while loop?
Correct Answer: B Option B correctly initializes 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.
7. Consider the following code segment:
int[] arr = {10, 5, 20, 25, 30};
int count = 0;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] % 10 == 0) {
        count++;
    }
}
System.out.println(count);
What is printed as a result of executing the code segment?
Correct Answer: B This loop counts how many elements are perfectly divisible by 10 (the remainder `%` is 0). The elements that satisfy this condition are 10, 20, and 30. Therefore, the count variable increments 3 times.
8. Consider the following code segment:
int[] myArr = {1, 2, 3};
for (int i = 0; i < myArr.length; i++) {
    myArr[i] = myArr[i] * 2;
}
System.out.print(myArr[2]);
What is printed as a result of executing the code segment?
Correct Answer: C This loop successfully modifies the elements inside the array. It visits each index and doubles the value stored there. The array is transformed from {1, 2, 3} to {2, 4, 6}. The value at index 2 is now 6.
9. Which of the following array traversals will result in an infinite loop?
Correct Answer: B In option B, the index variable 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.
10. Consider the following code segment:
int[] vals = {5, 2, 8, 3};
int max = vals[0];
for (int i = 1; i < vals.length; i++) {
    if (vals[i] > max) {
        max = vals[i];
    }
}
System.out.println(max);
What is printed as a result of executing the code segment?
Correct Answer: B This is a standard “Find the Maximum” algorithm. It initializes 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!

0 / 10

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.

← Back to Unit 6 Arrays Hub
Next: Topic 6.3 Enhanced For-Loops →

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline