AP CSA Unit 1 Day 27: Passing Object To Method Mutates State

Unit 1, Sections 1.5, 1.13 • Cycle 2
Day 27 Advanced Practice • Harder Difficulty Hard
Focus: Math.pow returns double, casting truncates, int division

Advanced Practice Question

Consider the following code segment. What is printed?
int x = (int) (Math.pow(3, 3) + 0.5);
double y = x / 10;
int z = x % 10;
System.out.println(x + " " + y + " " + z);
Why This Answer?

This combines three Unit 1 traps: Math return types, casting, and integer division.

Line 1: (int) (Math.pow(3, 3) + 0.5)

  → Math.pow(3, 3) returns 27.0 (Math.pow always returns double)

  → 27.0 + 0.5 = 27.5

  → (int) 27.5 = 27 (casting to int truncates, does not round)

Line 2: y = x / 10

  → x is int (27) and 10 is intinteger division

  → 27 / 10 = 2 (truncated)

  → The int result 2 is then stored in double yy = 2.0

Line 3: z = 27 % 10 = 7

Output: 27 2.0 7

The y prints as 2.0 because it is a double variable, even though the division produced an integer result.

Why Not the Others?

A) 27 2.7 7 — Assumes x / 10 produces 2.7. It does not, because x is int and 10 is int, so integer division happens first (result: 2), and only then is the int 2 widened to double 2.0.

B) 28 2.8 8 — Assumes (int) casting rounds 27.5 to 28. Casting to int always truncates (drops the decimal). 27.5 becomes 27, not 28.

D) 27 2 7 — Gets the math right but forgets that y is declared as double. When Java prints a double with value 2.0, it displays 2.0, not 2.

Common Mistake
Watch Out!

Three traps combined: (1) (int) casting truncates toward zero — it does not round. (int) 27.5 is 27, (int) 2.9 is 2. (2) int / int performs integer division before storing the result. Even though y is double, the division 27 / 10 already truncated to 2 before assignment. (3) A double variable always prints with a decimal point: 2.0, not 2.

AP Exam Strategy

When you see a double variable assigned from an int expression, the integer math happens first. To get 2.7, you would need at least one operand to be double: x / 10.0 or (double) x / 10. Also remember: casting with (int) always truncates. If the AP exam shows (int) 99.9, the answer is 99, not 100.

Keep Building Mastery!

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

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