Skip to main content
United StatesComputer ScienceSyllabus dot point

How are arithmetic expressions evaluated in Java, and what does integer division and the modulo operator do?

Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.

A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Arithmetic operators
  3. Operator precedence
  4. Integer division and modulo
  5. Assignment statements
  6. Try this

What this topic is asking

The College Board (Topic 1.3) wants you to evaluate arithmetic expressions correctly: apply operator precedence, understand that int / int performs integer division (truncation), use the modulo operator % to get a remainder, and assign the result to a variable. Getting integer division and % right is one of the most heavily tested skills in Unit 1.

Arithmetic operators

Operator precedence

Java evaluates expressions using precedence rules:

  1. Parentheses ( ) first.
  2. Multiplicative: *, /, %, evaluated left to right.
  3. Additive: +, -, evaluated left to right.

So 2 + 3 * 4 is 2 + 12 = 14, not 20. Use parentheses to force a different order: (2 + 3) * 4 is 20. When operators have equal precedence, Java works left to right.

Integer division and modulo

This is the single most error-prone idea in Unit 1. Examples:

  • 17 / 5 is 3 (not 3.4), because both are int.
  • 17 % 5 is 2 (the remainder).
  • 4 / 5 is 0 (truncated), and 4 % 5 is 4.
  • -7 / 2 is -3 (truncation is toward zero), and -7 % 2 is -1.

If you want a real-number result, make at least one operand a double: 17.0 / 5 is 3.4. Casting (Topic 1.5) is the usual way to do this with variables.

The modulo operator is the standard tool for extracting digits, testing divisibility (n % 2 == 0 means n is even), and wrapping values around a range.

Assignment statements

An assignment statement has the form variable = expression;. Java evaluates the right-hand side first, then stores that single value in the variable on the left. The = is assignment, not mathematical equality. Because the right side is evaluated first, a statement like x = x + 1; is legal: it reads the current x, adds 1, and writes the result back.

int x = 5;
x = x + 3;   // right side is 5 + 3 = 8, then stored: x is now 8

Try this

Q1. Evaluate 23 % 7 and 23 / 7. [2 points]

  • Cue. 23 / 7 is 3 (integer division); 23 % 7 is 2 (the remainder).

Q2. Explain why 1 / 2 * 8.0 evaluates to 0.0 rather than 4.0. [2 points]

  • Cue. Left to right, 1 / 2 is evaluated first as integer division, giving 0; then 0 * 8.0 is 0.0. The double appears too late to save the truncated 0.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. What is printed by the following statement? ```java System.out.println(7 + 3 * 2 - 8 / 3); ``` (A) `11` (B) `9` (C) `11.33` (D) `4` (E) `13`
Show worked answer →

The answer is (A).

Multiplication and division happen before addition and subtraction. 3 * 2 is 6. 8 / 3 is integer division, which truncates toward zero to 2 (not 2.67). The expression becomes 7 + 6 - 2, evaluated left to right: 13 - 2 = 11. (C) wrongly treats 8 / 3 as floating-point; (B) and (D) misapply precedence; (E) forgets to subtract the 2.

Markers reward applying precedence (multiplicative before additive) and remembering that int / int truncates.

AP 2020 (style)4 marksFree response (code writing). A total number of `cents` (an `int`, at least 0) is given. Write a code segment that uses integer division and the modulo operator to compute and print the number of whole dollars and the leftover cents, in the form `dollars = D, cents = C`. For example, if `cents` is 347, the output is `dollars = 3, cents = 47`.
Show worked answer →

A 4-point question testing integer division and modulo together.

int dollars = cents / 100;
int leftover = cents % 100;
System.out.println("dollars = " + dollars + ", cents = " + leftover);

Point 1: cents / 100 gives the whole dollars by integer division (347 / 100 is 3). Point 2: cents % 100 gives the remainder, the leftover cents (347 % 100 is 47). Point 3: storing each in an int (or using them directly). Point 4: printing in the exact format with concatenation. Using / on double values, or forgetting %, loses marks.

Related dot points

Sources & how we know this