Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.3 Expressions and Assignment Statements

1.3 Expressions and Assignment Statements

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

TLDR

In AP Computer Science A, expressions and output let your program calculate values and show them on screen. Use System.out.print to stay on the same line and System.out.println to move to a new line, and watch out for integer division, which throws away the decimal part when both numbers are ints.

How Do Expressions and Output Work in Java?

Expressions combine values, variables, and operators to produce a result, while output statements display that result. On the AP CSA exam, trace System.out.print, System.out.println, string concatenation, integer division, %, and operator precedence exactly in the order Java evaluates them.

Why This Matters for the AP Computer Science A Exam

Tracing expressions and predicting output is a core skill you will use across the whole course. Multiple-choice questions often hand you an arithmetic expression or a few output statements and ask for the exact result, so you need to apply operator precedence and integer division rules precisely. In free-response code writing, you will combine string literals with calculated values to display clear results, and the same arithmetic rules from this topic carry into loops, conditionals, and methods later on.

Key Takeaways

  • System.out.println moves the cursor to a new line after printing; System.out.print does not.
  • A string literal is text in double quotes, and escape sequences like \", \\, and \n let you include special characters.
  • An operation with two int values gives an int; if at least one value is a double, the result is a double.
  • Integer division keeps only the whole-number part, and % gives the remainder.
  • Multiplication, division, and remainder happen before addition and subtraction, and same-level operators evaluate left to right; parentheses override this.
  • Dividing an integer by integer zero throws an ArithmeticException.

Output Methods: print vs println

Java gives you two main ways to display output: System.out.print() and System.out.println(). The difference is simple but important. print continues on the same line, while println moves the cursor to a new line after it prints.

</>Java
System.out.print("Hello");
System.out.print("World");
// Output: HelloWorld

System.out.println("Hello");
System.out.println("World");
// Output: Hello
//         World

Use println when you want each piece of information on its own line. Use print when you are building up a single line of output piece by piece.

String Literals and Escape Sequences

A literal is the code representation of a fixed value. A string literal is a sequence of characters enclosed in double quotes. But what if you need to include a quote mark inside your string, or a new line? That is where escape sequences come in.

The backslash \ is the escape character. It tells Java that the next character is special. The escape sequences used in this course are \" for a double quote, \\ for a backslash, and \n for a newline.

</>Java
System.out.println("She said \"Hello\" to me");
// Output: She said "Hello" to me

System.out.println("Line 1\nLine 2\nLine 3");
// Output: Line 1
//         Line 2
//         Line 3

System.out.println("Path: C:\\Users\\Documents");
// Output: Path: C:\Users\Documents

Arithmetic Operators and Basic Math

Java handles math much like you would expect, with one major catch covered in the next section. The arithmetic operators are + (addition), - (subtraction), * (multiplication), / (division), and % (remainder).

</>Java
System.out.println(5 + 3);    // 8
System.out.println(10 - 7);   // 3

System.out.println(4 * 6);    // 24
System.out.println(15.0 / 4); // 3.75
System.out.println(17 % 5);   // 2

An arithmetic operation that uses two int values evaluates to an int value. An operation that uses at least one double value evaluates to a double value. The remainder operator % gives what is left over after division: 17 divided by 5 is 3 with 2 left over, so 17 % 5 equals 2.

Integer Division: The Silent Truncator

This is where students often lose points. When you divide two int values, Java keeps only the integer portion of the quotient. There is no rounding, just truncation of the decimal part.

</>Java
System.out.println(7 / 2);     // 3, not 3.5
System.out.println(10 / 3);    // 3, not 3.333...
System.out.println(1 / 2);     // 0

// Fix: make at least one number a double
System.out.println(7.0 / 2);   // 3.5
System.out.println(10 / 3.0);  // 3.333...

This is expected behavior, not a bug. Sometimes you want integer division, like finding how many full groups you can make. But if you need the decimal, make at least one value a double.

One more rule to remember: dividing an integer by the integer zero throws an ArithmeticException. Dividing by zero when one value is a double is outside the scope of this course.

Operator Precedence: Who Goes First?

Java follows the same order of operations you learned in math. Multiplication, division, and remainder happen before addition and subtraction. Operators at the same precedence level evaluate left to right.

</>Java
System.out.println(2 + 3 * 4);      // 14, not 20
System.out.println(10 - 6 / 2);     // 7, not 2

System.out.println(8 / 4 * 2);      // 4 (left to right)
System.out.println(15 % 4 + 2);     // 5 (% same level as * and /)

When in doubt, use parentheses. They make your code clearer and guarantee the grouping you want.

Building Complex Output

You can combine variables, literals, and expressions to create meaningful output. Notice how print and println are used together here.

</>Java
int score = 95;
int bonus = 10;
System.out.print("Your score: ");
System.out.println(score);
System.out.print("With bonus: ");
System.out.println(score + bonus);
// Output: Your score: 95
//         With bonus: 105

Calculating and Displaying Results

Break down the calculation, then format the output.

</>Java
// Calculate average of three test scores
int test1 = 85;
int test2 = 92;
int test3 = 88;

// Step 1: Calculate sum
int sum = test1 + test2 + test3;

// Step 2: Calculate average (watch that integer division)
double average = sum / 3.0;  // Use 3.0 to force double division

// Step 3: Display results
System.out.println("Test scores: " + test1 + ", " + test2 + ", " + test3);
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);

Working with Remainders

The remainder operator is useful for checking divisibility and extracting digits.

</>Java
// Check if a number is even
int num = 17;
System.out.print(num + " is ");
if (num % 2 == 0) {
    System.out.println("even");
} else {
    System.out.println("odd");
}

// Extract digits from a number
int value = 256;
int ones = value % 10;        // 6
int tens = (value / 10) % 10; // 5
int hundreds = value / 100;   // 2
System.out.println("Digits: " + hundreds + " " + tens + " " + ones);

How to Use This on the AP Computer Science A Exam

Code Tracing

For expression questions, write out each step instead of doing it all in your head. The exact result matters, including integer division truncation. When you see division, immediately check whether both operands are int values. If you need a decimal result, make sure at least one operand is a double.

A common multiple-choice trap mixes int and double division in one expression. For 25 / 4 * 1.0, work left to right: 25 / 4 is 6 because both are ints, then 6 * 1.0 is 6.0. Another favorite tests escape sequences, like asking how many lines "Line1\nLine2" prints. Each \n starts a new line.

Free Response

In free-response code writing, you often need to display calculated results with labels, like "The average is: " + average. When you concatenate anything with a string, the result becomes a string, which is useful for combining numbers and text. Watch the order: "Sum: " + 3 + 4 produces "Sum: 34", but "Sum: " + (3 + 4) produces "Sum: 7" because the parentheses force the addition first.

Common Trap

Forcing the right division type catches most mistakes. For a percentage, correct / total * 100 with two ints can give 0 before it ever multiplies. Cast first with (double) correct / total * 100 to get the real value. For output questions, trace line by line and count newlines carefully, remembering that println adds one at the end and print does not.

Practice Problems

Problem 1: Temperature Converter

Convert 86 degrees Fahrenheit to Celsius and display both values. Formula: C = (F - 32) × 5/9.

</>Java
// Solution:
int fahrenheit = 86;
double celsius = (fahrenheit - 32) * 5.0 / 9.0;  // Use 5.0 and 9.0

System.out.println(fahrenheit + " F = " + celsius + " C");
// Output: 86 F = 30.0 C

Use 5.0 / 9.0 to avoid integer division. With 5 / 9, you would get 0.

Problem 2: Time Display

Given total seconds, display it as hours:minutes:seconds.

</>Java
int totalSeconds = 7265;

// Solution approach: divide and use remainder
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = totalSeconds % 60;

System.out.print("Time: ");
System.out.print(hours + ":");
System.out.print(minutes + ":");
System.out.println(seconds);
// Output: Time: 2:1:5

First extract hours, then work with what is left using division and remainder.

Problem 3: Receipt Calculator

Calculate and display a receipt with subtotal, tax, and total.

</>Java
double item1 = 12.99;
double item2 = 24.50;
double item3 = 8.75;
double taxRate = 0.08;

double subtotal = item1 + item2 + item3;
double tax = subtotal * taxRate;
double total = subtotal + tax;

System.out.println("Receipt");
System.out.println("-------");
System.out.println("Item 1: $" + item1);
System.out.println("Item 2: $" + item2);
System.out.println("Item 3: $" + item3);
System.out.println("Subtotal: $" + subtotal);
System.out.println("Tax: $" + tax);
System.out.println("Total: $" + total);

Common Misconceptions

  • println does not add a blank line by itself; it just ends the current line so the next output starts fresh on the next line.
  • Integer division does not round. 7 / 2 is 3, not 4, because the decimal part is dropped.
  • Making the result variable a double does not fix integer division. double avg = 7 / 2; still stores 3.0 because the division happened with two ints first.
  • % is the remainder operator, not a percent sign. 17 % 5 is 2, the amount left over.
  • \n is a single escape sequence for a newline, not a backslash followed by a separate letter n.
  • Order matters with + and strings. 3 + 4 + " is sum" gives "7 is sum" because the two ints add first, then concatenate with the string.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

addition

The arithmetic operator (+) that combines two numeric values to produce their sum.

arithmetic expression

Mathematical expressions consisting of numeric values, variables, and operators that perform calculations.

ArithmeticException

An exception that occurs when attempting to divide an integer by zero.

compound expression

Expressions that use multiple operators to combine numeric values.

division

The arithmetic operator (/) that divides one numeric value by another.

double

A primitive data type used to store real numbers (numbers with decimal points).

escape sequence

A special sequence of characters starting with a backslash that has a special meaning in Java, such as \" for double quote, \\ for backslash, and \n for newline.

int

A primitive data type used to store integer values (whole numbers).

integer division

Division of two int values that results in only the integer portion of the quotient, discarding any fractional part.

literal

The code representation of a fixed value.

multiplication

The arithmetic operator (*) that produces the product of two numeric values.

numeric value

Numbers used in expressions, which can be of type int or double.

operator

Symbols that perform operations on numeric values in expressions.

operator precedence

The rules that determine the order in which operators are evaluated in an expression, with multiplication, division, and remainder evaluated before addition and subtraction.

parentheses

Symbols used to modify operator precedence and control the order of operations in an expression.

remainder operator

The operator (%) that computes the remainder when one number is divided by another.

string literal

A sequence of characters enclosed in double quotes used to create a String object directly in code.

subtraction

The arithmetic operator (-) that finds the difference between two numeric values.

System.out.print

A Java method that displays information on the computer display without moving the cursor to a new line after the output.

System.out.println

A Java method that displays information on the computer display and moves the cursor to a new line after the output.

variable

A named storage location in a program that holds a value of a specific data type; the value can change while the program is running.

Frequently Asked Questions

What is the difference between System.out.print and System.out.println?

System.out.print displays output and stays on the same line. System.out.println displays output and then moves to a new line.

What is a string literal in Java?

A string literal is fixed text written inside double quotes, such as "Hello". Escape sequences like " , \ , and let you include quotes, backslashes, and new lines inside strings.

How does integer division work in Java?

When both operands are int values, division keeps only the integer part of the quotient. For example, 7 / 2 evaluates to 3, not 3.5.

What does the remainder operator do?

The % operator gives the remainder after integer division. For example, 17 % 5 is 2 because 17 divided by 5 leaves 2 left over.

What is operator precedence in AP CSA?

Java evaluates multiplication, division, and remainder before addition and subtraction. Operators at the same level evaluate left to right, and parentheses can change the grouping.

How does Topic 1.3 show up on the AP CSA exam?

Topic 1.3 appears in output tracing and expression-evaluation questions. Carefully track print versus println, string concatenation, integer division, remainder, and operator precedence.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot