AP CSA Unit 1.5: Type Casting and Truncation Practice
Share
Unit 1, Section 1.5
Day 5 Practice • January 11, 2026
🎯 Focus: Type Casting
Practice Question
Consider the following code segment:
double d = 7.9;
int x = (int) d;
double y = x + 0.5;
System.out.println(y);
What is printed as a result of executing this code segment?
What This Tests: Section 1.5 covers casting and range of variables. Casting double to int truncates (chops off) the decimal—it does NOT round. Understanding this distinction is critical for the AP exam.
Key Concept: Casting Truncates
When you cast a double to an int, Java truncates (removes) the decimal portion. It does NOT round.
(int) 7.9 → 7 // NOT 8 (would be rounded)
(int) 7.1 → 7
(int) -3.7 → -3 // Truncates toward zero
(int) 9.99 → 9 // Still just 9!
Step-by-Step Trace
| Line | Code | d | x | y |
|---|---|---|---|---|
| 1 | double d = 7.9; | 7.9 | - | - |
| 2 | int x = (int) d; | 7.9 | 7 | - |
| 3 | double y = x + 0.5; | 7.9 | 7 | 7.5 |
Output: 7.5 (7 + 0.5 = 7.5)
Common Mistakes
Mistake: Answer A (8.4)
This assumes rounding (7.9 → 8) then 8 + 0.5 = 8.5... but that's not even 8.4. Likely arithmetic error combined with rounding assumption.
Mistake: Answer E (8.5)
This comes from rounding 7.9 to 8, then adding 0.5. But casting truncates—7.9 becomes 7, not 8!
Automatic vs Explicit Casting
Casting Rules
Automatic (widening): int → double happens automaticallydouble y = x + 0.5; // x (7) becomes 7.0 automatically
Explicit (narrowing): double → int requires castint x = (int) d; // Must explicitly cast
Related Topics
- Section 1.2: Variables and Data Types
- Section 1.3: Expressions (mixing types)
- Section 1.11: Math Class (Math.round())
Difficulty: Medium • Time: 2-3 minutes • AP Skill: 2.A - Apply operators
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 Tutoring300+ Unit 1 questions • Expert tutoring • 5.0 rating