AP CSA Unit 1.2: Integer Division and Data Types Practice

Unit 1, Section 1.2
Day 2 Practice • January 8, 2026
🎯 Focus: Variables and Data Types

Practice Question

Consider the following code segment:
int x = 13;
int y = 5;
double result = x / y;
System.out.println(result);
What is printed as a result of executing this code segment?

What This Tests: Section 1.2 covers variables and data types. This question specifically tests integer division—one of the most commonly missed concepts on the AP exam. When you divide two integers in Java, the result is always an integer with the decimal portion truncated (not rounded).

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 / 52    // Not 2.6, not 3
7 / 23    // Not 3.5
9 / 100    // Not 0.9
15 / 43    // 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

Mistake 1: Answer A (2.6)

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.

Mistake 2: Answer C (2)

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.

Mistake 3: Answer D (3.0)

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.

Mistake 4: Answer E (2.5)

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

The Division Type Check

Before calculating any division, ask:

  1. What type is the LEFT operand? (int or double?)
  2. What type is the RIGHT operand? (int or double?)
  3. If BOTH are int → Integer division (truncate decimal)
  4. 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

Example 1: Order matters for casting
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)
Example 2: Common real-world bug
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 (/=)
Difficulty: Medium • Time: 2-3 minutes • AP Skill: 2.A - Apply the meaning of 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 Tutoring

300+ Unit 1 questions • Expert tutoring with 1,700+ hours experience • 5.0 rating

Back to blog

Leave a comment

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