• 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: Enhanced For-Loops

Home » AP CSA MCQ » Unit 6

AP CSA Practice: Enhanced For-Loops

📚 Topic 6.3 ⏱️ 10 Min Practice ✅ Updated for 2027 Exam

Once you master standard for and while loops, Java offers a powerful, streamlined alternative: the enhanced for-loop (often called the “for-each” loop).

In this interactive practice set, we cover Topic 6.3: Enhanced For-Loops. This loop structure is specifically designed to traverse arrays cleanly and safely without ever having to worry about an ArrayIndexOutOfBoundsException. However, the College Board loves to test students on its strict limitations.

Key Concepts to Remember Before You Start:

  • Read-Only Nature: An enhanced for-loop makes a copy of each primitive element. If you change the loop variable, you do not change the actual array.
  • No Index Access: You do not have access to an i counter variable. If you need to know the specific index of an element, you must use a standard for loop.
  • No Partial Traversals: An enhanced for-loop always starts at index 0 and visits every single element from left to right. You cannot step by twos or go backward.
  • Syntax: The format is always for (DataType variableName : arrayName).

Test your understanding of these critical rules with the 10 practice questions below!

Topic 6.3 Practice

Question 1 of 10 | Score: 0
1. Which of the following is the correct syntax to use an enhanced for-loop to traverse an array of double values named prices?
Correct Answer: A The correct syntax for an enhanced for-loop in Java is for (DataType variableName : arrayName). The data type of the loop variable must match the elements stored inside the array. Option B uses “in”, which is syntax from Python, not Java.
2. Consider the following code segment:
int[] scores = {80, 90, 100};
for (int s : scores) {
    s = s + 5;
}
System.out.println(scores[0]);
What is printed as a result of executing the code segment?
Correct Answer: A This is the most important rule of enhanced for-loops: the loop variable (s) is just a copy of the array element. Modifying s changes the local variable, but it does absolutely nothing to the original scores array. The array remains {80, 90, 100}.
3. Consider the following code segment:
int[] numbers = {1, 2, 3, 4};
int total = 0;
for (int n : numbers) {
    total += n;
}
System.out.println(total);
What is printed as a result of executing the code segment?
Correct Answer: B This loop successfully traverses the entire array from left to right. It adds each element (1, 2, 3, 4) to the total accumulator variable. 1 + 2 + 3 + 4 = 10.
4. Which of the following tasks CANNOT be easily accomplished using a standard enhanced for-loop without adding extra tracking variables?
Correct Answer: D Because the loop variable in an enhanced for-loop is a read-only copy, you cannot modify the original array elements directly. To replace negative values with zero, you need to access the array via its index (e.g., arr[i] = 0), which requires a standard for loop.
5. Consider the following code segment:
String[] words = {“cat”, “dog”, “bird”};
for (String w : words) {
    w = w.toUpperCase();
}
System.out.println(words[0]);
What is printed as a result of executing the code segment?
Correct Answer: A Just like with primitives, the enhanced for-loop creates a copy of the reference. Furthermore, Strings in Java are immutable. Reassigning w to point to a new uppercase String object does not change the reference stored inside the original array. The array remains unchanged.
6. A programmer wants to traverse an array and print only the elements located at even indices (index 0, 2, 4, etc.). Which loop is best suited for this task?
Correct Answer: B Enhanced for-loops cannot perform partial traversals or skip elements. They must visit every single element from left to right. A standard for-loop is required because it allows you to control the counter variable (i += 2) to jump directly to even indices.
7. Consider the following code segment:
int[] arr = {5, -2, 8, -1, 4};
int c = 0;
for (int num : arr) {
    if (num > 0) {
        c++;
    }
}
System.out.print(c);
What is printed as a result of executing the code segment?
Correct Answer: B This algorithm correctly uses an enhanced for-loop to count specific elements. It checks every element to see if it is strictly greater than 0 (positive). The positive numbers are 5, 8, and 4. The counter increments 3 times.
8. What common runtime error is essentially impossible to trigger when using an enhanced for-loop to traverse a fully initialized array?
Correct Answer: C Because the enhanced for-loop automatically handles the boundary conditions (starting at 0 and ending at length - 1) behind the scenes, you cannot accidentally code an “off-by-one” error. It guarantees you will not get an ArrayIndexOutOfBoundsException.
9. Consider the following code segment:
String[] letters = {“A”, “B”, “C”};
String result = “”;
for (String letter : letters) {
    result = letter + result;
}
System.out.println(result);
What is printed as a result of executing the code segment?
Correct Answer: B Pay close attention to the order of string concatenation. The code states result = letter + result;. This means the new letter is prepended to the *front* of the string. Iteration 1: “A”. Iteration 2: “B” + “A” = “BA”. Iteration 3: “C” + “BA” = “CBA”.
10. In which of the following scenarios is an enhanced for-loop the MOST appropriate choice?
Correct Answer: D An enhanced for-loop is perfect for “read-only” traversals where you need to examine or print every single item. Options A and C require modifying the array, which a for-each loop cannot do. Option B requires returning an index, which a for-each loop does not track.

Unit 6.3 Practice Complete!

0 / 10

Why Can’t Enhanced For-Loops Modify Arrays?

The most common trap students fall into on the AP exam is trying to write code like for (int x : array) { x = 0; } to reset an array. Why doesn’t this work?

When an enhanced for-loop runs, Java creates a brand new, temporary variable in memory (in this case, x). It copies the value from the array into x. When you set x = 0, you are only changing that temporary container. The original array remains completely untouched. If you need to change data or access specific indexes, you must rely on a standard for loop.

Ready for the Next Step?

Now that you have mastered creating arrays and traversing them with both standard and enhanced loops, it is time to put those skills together to build the actual algorithms tested on the AP exam.

Continue your AP CSA review by moving on to Topic 6.4: Standard Array Algorithms Practice, where we cover shifting, reversing, finding minimums, and calculating averages.

← Back to Unit 6 Arrays Hub
Next: Topic 6.4 Standard Algorithms →

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