Casting and Range of Variables
AP CSA Unit 1.5 — Casting and Range of Variables (2025)
Scope: Java casting between int and double, integer division, modulus, and range/overflow behavior of int.
Goal: Predict results, spot overflow, and place casts correctly to control evaluation.
Practice: predict the output (Java)
Write your answers, then check against the key below.
Q1
int a = 10, b = 4;
System.out.println(a / b);
System.out.println(a % b);
System.out.println((double)(a / b));
System.out.println((double)a / b);
2
2
2.0
2.5
Q2
double d = -2.6;
System.out.println((int)d);
System.out.println((int)(d - 0.5));
System.out.println((int)(-d + 0.5));
-2
-3
3
Q3
int x = Integer.MAX_VALUE;
int y = x + 2;
System.out.println(x);
System.out.println(y);
2147483647
-2147483647
Answer key
Q1
2
2
2.0
2.5
Q2
-2
-3
3
Q3
2147483647
-2147483647 // wraps: MAX_VALUE + 2
FRQ-style tasks
FRQ 1. Average with correct casting
Write a method avgInt that takes two int values and returns their average as a double, preserving the .5 if present.
public static double avgInt(int a, int b) {
return (a + b) / 2.0;
}
avgInt(3, 5); // returns 4.0
4.0
FRQ 2. Percentage
Given int correct and int total, compute the percentage as a double from 0.0 to 100.0 without losing fractional precision.
public static double percent(int correct, int total) {
return (double) correct / total * 100;
}
percent(45, 50); // returns 90.0
90.0
FRQ 3. Safe remainder
Implement safeMod(int a, int b) that returns a % b, but if b == 0, it should return 0 instead of throwing.
public static int safeMod(int a, int b) {
if (b == 0) {
return 0;
}
return a % b;
}
safeMod(5, 0); // returns 0
0
Java Casting MCQ’s
Here are 5 multiple-choice questions on Java casting:
Question 1: What is the output of the following code?
A) 7.9
B) 8
C) 7
D) 8.0
E) Compile error
double x = 7.9;
int y = (int) x;
System.out.println(y);
7
Question 2: Which of the following statements will compile without error?
A) int c = a + b;
B) int c = (int)(a + b);
C) int c = a + (int)b;
D) Both B and C
E) All of the above
int a = 10;
double b = 5.5;
int c = (int)(a + b);
int c = a + (int)b;
int c = a + b;
| int c = a + b;
incompatible types: possible lossy conversion from double to int
Both B and C compile without error. Option A causes the following error:
| int c = a + b; incompatible types: possible lossy conversion from double to int
Question 3:
What is the result of the following code?
A) 4.5
B) 4
C) 5
D) 4.0
E) Compile error
int num = 9 / 2;
System.out.println(num);
4
Question 4: Given the following code, what is printed?
A) 15
B) 16
C) 15.5
D) 16.0
E) 15.7
double d = 15.7;
int i = (int)(d + 0.5);
System.out.println(i);
16
Question 5: Which statement about casting is TRUE?
A) Casting from int to double requires explicit casting
B) Casting from double to int requires explicit casting
C) Casting from int to double may result in loss of data
D) Casting is never necessary in Java
E) Casting from double to int always rounds to the nearest integer
The correct answer is B. This is because double → int is a narrowing conversion and requires an explicit cast.