Skip to main content
← AP CSA Study Guide

Code Scholars Tutoring · AP Computer Science A

1.1: Primitive Expressions

The operand types determine arithmetic before assignment takes place. Multiplication and division precede addition; parentheses change grouping.

College Board Unit 1

How it works

Primitive expressions are where every AP CSA program starts. Java evaluates arithmetic using the types of the operands, not the type of the variable you are assigning into, so int / int stays an int and double arithmetic keeps its decimal part. Precedence follows the usual order: multiplication, division, and mod before addition and subtraction, with parentheses overriding everything. Compound assignment operators such as += and *= apply the operation and reassign in one step.

Evaluate an expression by resolving parentheses, then multiplication and division, then addition and subtraction, recording the type at each step.

Common error

Assuming every division keeps its decimal part. Two ints divide to an int no matter what the surrounding expression looks like.

Worked example

Java

int a = 9;
double result = a / 2 + 0.5;
System.out.println(result);

Output

4.5

Both operands of 9 / 2 are integers, so that subexpression is 4. Adding 0.5 then produces a double.

Exercises

1.1.1 · Trace

Determine the exact output. Explain the Java rule or state changes that produce it.

Java

int n = 4;
n += 3 * 2;
System.out.println(n - 1);
Show solution

Output

9

Multiplication produces 6, compound assignment changes n to 10, and subtraction prints 9.

1.1.2 · Write code

Given int earned = 17 and int possible = 20, calculate the percentage as a double and print it. Preserve fractional division.

Show solution

One valid solution is shown below. Other implementations satisfying the contract are also valid.

Java

int earned = 17;
int possible = 20;
double percent = 100.0 * earned / possible;
System.out.println(percent);
Practice this topic: MCQ →

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic