AP CSA Unit 1.2: Integer Division and Data Types Practice
Share
Practice Question
int x = 13;
int y = 5;
double result = x / y;
System.out.println(result);
Key Concept: Integer Division
Integer division occurs when you divide two int values. Java performs the division and discards (truncates) any decimal portion—it does NOT round.
// Integer division examples
13 / 5 → 2 // Not 2.6, not 3
7 / 2 → 3 // Not 3.5
9 / 10 → 0 // Not 0.9
15 / 4 → 3 // Not 3.75
Critical insight: The division happens FIRST (producing 2), THEN the result is stored in the double variable. Storing 2 in a double gives you 2.0, but you can't recover the lost decimal portion.
Step-by-Step Trace
| Line | Code | What Happens | Result |
|---|---|---|---|
| 1 | int x = 13; | Store 13 in x | x = 13 |
| 2 | int y = 5; | Store 5 in y | y = 5 |
| 3 | double result = x / y; | 13 / 5 = 2 (int ÷ int = int) Then 2 is converted to 2.0 |
result = 2.0 |
| 4 | println(result); | Print result | 2.0 |
Common Mistakes
This would be correct if Java did true mathematical division. But since both x and y are integers, Java performs integer division FIRST (13/5 = 2), THEN converts to double (2.0). The .6 is already gone before the assignment to result.
Close! The division does produce 2, but since result is declared as a double, Java converts the integer 2 to the double 2.0. Doubles always display with a decimal point.
This would be correct if Java rounded the result, but integer division truncates (chops off the decimal), it doesn't round. 13/5 = 2.6 truncated = 2, not 3.
This confuses 13/5 with 5/2. Make sure to read which number is the dividend (being divided) and which is the divisor (dividing by).
How to Get True Division
If you WANT the decimal portion, at least one operand must be a double:
// Method 1: Cast one operand
double result = (double) x / y; // 2.6
// Method 2: Make one number a double literal
double result = x / 5.0; // 2.6
// Method 3: Declare variables as double
double x = 13;
double y = 5;
double result = x / y; // 2.6
Practice Technique
Before calculating any division, ask:
- What type is the LEFT operand? (int or double?)
- What type is the RIGHT operand? (int or double?)
- If BOTH are int → Integer division (truncate decimal)
- If EITHER is double → True division (keep decimal)
The type of the RESULT variable doesn't matter for the division itself—only the operands matter!
Additional Examples
int a = 7, b = 2;
double r1 = (double) (a / b); // 3.0 (int division first, then cast)
double r2 = (double) a / b; // 3.5 (cast first, then division)
int correct = 7;
int total = 10;
double percent = correct / total * 100;
System.out.println(percent); // 0.0 (not 70.0!)
// Fix: cast first
double percent = (double) correct / total * 100; // 70.0
Related Topics
- Section 1.3: Expressions and Output (arithmetic operations)
- Section 1.5: Casting and Range of Variables (type conversion)
- Section 1.6: Compound Assignment 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 with 1,700+ hours experience • 5.0 rating