AP CSA Unit 4.4: Array Traversal and Element Modification Practice
Share
Unit 4, Section 4.4
Day 4 Practice • January 10, 2026
🎯 Focus: Array Traversals
Practice Question
Consider the following code segment:
int[] nums = {5, 10, 15, 20, 25};
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i] * 2;
}
System.out.println(nums[2]);
What is printed as a result of executing this code segment?
What This Tests: Section 4.4 covers array traversals. Use an indexed for loop when you need to MODIFY array elements. The enhanced for loop cannot modify elements!
Step-by-Step Trace
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Before | 5 | 10 | 15 | 20 | 25 |
| After *2 | 10 | 20 | 30 | 40 | 50 |
nums[2] = 30 (was 15, now doubled)
When to Use Each Loop
// INDEXED for loop - use when MODIFYING
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2; // This WORKS
}
// ENHANCED for loop - use when READING only
for (int n : arr) {
n = n * 2; // This does NOT modify arr!
}
Common Mistakes
Mistake: Answer A (15)
This is the ORIGINAL value at index 2. But the loop doubles every element, so nums[2] becomes 15 * 2 = 30.
Off-by-One: Index 2 vs Element 2
Index 2 is the THIRD element (0, 1, 2). The original third element is 15, which becomes 30.
Difficulty: Medium • Time: 2-3 minutes • AP Skill: 2.D - Traverse arrays
Ready to Level Up Your AP CSA Skills?
Get personalized help or access our complete question bank
Premium Question Bank - Coming Soon! Schedule 1-on-1 Tutoring