AP CSA Unit 4.2: Array Averaging with Enhanced For Loop Practice

Unit 4, Section 4.2
Day 2 Practice • January 8, 2026
🎯 Focus: Data Sets and Averaging

Practice Question

Consider the following code segment:
int[] temps = {72, 68, 75, 80, 77};
int sum = 0;

for (int t : temps) {
    sum += t;
}

double avg = (double) sum / temps.length;
System.out.println(avg);
What is printed as a result of executing this code segment?

What This Tests: Section 4.2 covers working with data sets. This question tests the enhanced for loop for array traversal and the proper technique for calculating an average with double division.

Step-by-Step Trace

Iteration t sum += t sum
1 72 0 + 72 72
2 68 72 + 68 140
3 75 140 + 75 215
4 80 215 + 80 295
5 77 295 + 77 372

Average: (double) 372 / 5 = 74.4

Key Concept: Casting for Division

// Without cast: integer division!
double avg = sum / temps.length;  // 372/5 = 74 → 74.0

// With cast: true division
double avg = (double) sum / temps.length;  // 372.0/5 = 74.4

Common Mistakes

Mistake: Answer A (74.0)

This would be correct WITHOUT the cast. But (double) sum converts 372 to 372.0 before dividing, giving 74.4, not 74.0.

Mistake: Answer D (372)

This is the sum, not the average. Don't forget to divide by temps.length!

Enhanced For Loop

for-each Syntax

for (int t : temps) means "for each int t in temps"

Use when you need to READ all elements but don't need the index.

Cannot modify array elements with enhanced for loop!

Difficulty: Medium • Time: 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
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.