AP CSA Casting Range Pitfalls
Casting and Range Pitfalls in AP CSA: Complete Guide (2025-2026)
Casting and range pitfalls in AP CSA are responsible for some of the trickiest MCQ questions on the exam — code that compiles cleanly but produces a completely unexpected result due to integer division truncation, narrowing casts, or numeric overflow. Unit 1 (15–25%) establishes these rules, and they resurface constantly in Unit 2 loops and Unit 4 array computations. Understanding exactly when Java silently drops the decimal, silently wraps a value, or silently changes a type is the difference between a 4 and a 5 on this exam.
📄 Table of Contents
💻 Code Examples — Predict First
Before running each example, write down your prediction. This is the single most effective AP exam study technique.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
int a = 7;
int b = 2;
double result1 = a / b; // int division first!
double result2 = (double) a / b;
System.out.println(result1);
System.out.println(result2);
}
}
3.0 / 3.5 — result1: 7/2 is computed as INT division first (= 3), THEN assigned to double (3.0). The cast must happen BEFORE division. result2: (double)a makes it 7.0/2 = 3.5.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
double d = 9.99;
int i = (int) d; // narrowing cast: truncates
System.out.println(i);
int x = (int) -3.9; // truncates toward zero
System.out.println(x);
}
}
9 / -3 — (int)9.99 truncates toward zero = 9. (int)-3.9 also truncates toward zero = -3, NOT -4. Casting never rounds — it always chops the decimal.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
int big = Integer.MAX_VALUE;
System.out.println(big);
System.out.println(big + 1); // overflow!
byte b = (byte) 200; // narrowing overflow
System.out.println(b);
}
}
2147483647 / -2147483648 / -56 — MAX_VALUE + 1 wraps to MIN_VALUE (overflow). (byte)200 overflows the byte range (-128 to 127) and wraps to -56. The AP exam tests overflow awareness, not the exact wrap value.
❌ Common Pitfalls
These are the mistakes students most often make on the AP CSA exam with casting AP CSA int division truncation. Study them carefully.
When both operands are int, Java performs integer division and discards the decimal. 7/2 is 3, not 3.5. This is the #1 most common casting trap on the AP exam. To force floating-point division, cast at least one operand before the division.
int a=7, b=2; double r = a / b; // 3.0 (truncated first!) double r = (double)a / b; // 3.5 (cast BEFORE dividing)
(int) casting removes the fractional part, always moving toward zero. So (int)3.9 = 3 and (int)-3.9 = -3 (NOT -4). Students who think of it as 'rounding down' get negative values wrong.
(int) 3.9 // = 3 (toward zero) (int)-3.9 // = -3 (toward zero, NOT -4)
Assigning a double to an int variable without an explicit cast is a COMPILE ERROR. Java will not silently narrow types on assignment. You must write int x = (int) 3.7;
int x = 3.7; // COMPILE ERROR int x = (int) 3.7; // OK, x = 3
int → double is widening (automatic, no cast needed). double → int is narrowing (requires explicit cast). The AP exam tests both directions.
double d = 5; // OK: widening (int -> double) int i = 5.0; // COMPILE ERROR: narrowing needs cast
On the AP exam, whenever you see division with two int variables assigned to a double, immediately check: was either operand cast to double BEFORE the division? If not, the result is truncated integer division stored in a double. This single rule explains dozens of MCQ traps.
The expression (double)(a/b) is NOT the same as (double)a/b. The first computes int division THEN casts the result. The second casts a before dividing, forcing floating-point division. The parentheses placement is everything.
✍ Check for Understanding (8 Questions)
result?double result = 5 / 2;
System.out.println((int) 7.9);
(int) -4.1?
(double)(7/2) evaluate to?
I. double r = (double)5/2;II. double r = 5/(double)2;III. double r = 5/2;
int x = 1000000;
int y = x * x;
System.out.println(y > 0);
❓ Frequently Asked Questions
When both operands are int, Java performs integer division and discards the fractional part. This is not rounding — it is truncation. To get 3.5, at least one operand must be a double before the division: (double)7/2 or 7/2.0.
A cast (int), (double) is an explicit type conversion instruction. Widening conversions (int to double) happen automatically. Narrowing conversions (double to int) require an explicit cast and truncate the decimal part.
It always truncates toward zero. (int)3.9 = 3, (int)3.1 = 3, (int)-3.9 = -3 (not -4). Think of it as removing everything after the decimal point, regardless of direction.
Integer overflow occurs when a computation exceeds the int range (-2,147,483,648 to 2,147,483,647). The value wraps around to the other end of the range. The AP exam tests awareness of overflow, not the exact wrapped value.
Widening: assigning a smaller type to a larger type (int to double) — automatic, no cast needed. Narrowing: assigning a larger type to a smaller type (double to int) — requires explicit (int) cast and loses the fractional part.
Tanner Crow — AP CS Teacher & Tutor
11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.
- 54.5% of students score 5 on AP CSA (national avg: 25.5%)
- 1,845+ verified tutoring hours • 5.0 rating
- Free 1-on-1 tutoring inquiry: Wyzant Profile
🔗 Related Topics
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]