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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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:
- Parentheses
( )first. - Multiplicative:
*,/,%, evaluated left to right. - 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 / 5is3(not3.4), because both areint.17 % 5is2(the remainder).4 / 5is0(truncated), and4 % 5is4.-7 / 2is-3(truncation is toward zero), and-7 % 2is-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 / 7is3(integer division);23 % 7is2(the remainder).
Q2. Explain why 1 / 2 * 8.0 evaluates to 0.0 rather than 4.0. [2 points]
- Cue. Left to right,
1 / 2is evaluated first as integer division, giving0; then0 * 8.0is0.0. Thedoubleappears too late to save the truncated0.
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
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 1.4 Compound Assignment Operators: use the compound assignment operators (+=, -=, *=, /=, %=) and the increment and decrement operators (++, --) as shorthand to update variables.
A focused answer to AP CSA Topic 1.4, covering the compound assignment operators, the increment and decrement operators, how each rewrites a longer assignment, and the integer-division traps they can hide, with a traced worked example.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
- Topic 1.1 Why Programming? Why Java?: explain how a program is a sequence of statements executed in order, how Java source is compiled and run, and how to display output and handle simple runtime behavior.
A focused answer to AP CSA Topic 1.1, covering what a program is, the structure of a Java class with a main method, sequential execution, System.out output, and compile-time versus run-time errors, with a fully traced worked program.
- Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)