Expressions and Assignment Statements

Emily Wilson
8 min read
Study Guide Overview
This study guide covers numeric types in Java, including int and double, and basic math operations. It explains the assignment operator, incrementing/decrementing, the modulo operator, and division. It emphasizes the order of operations (PEMDAS) and includes practice problems on basic math, modulo, and AP-style questions. The guide also highlights common mistakes and provides exam tips focusing on integer division and data types.
#AP Computer Science A: Expressions and Assignment Statements - The Night Before 🚀
Hey! Let's make sure you're totally ready for tomorrow. This guide will help you nail those tricky questions on expressions and assignment statements. Let's dive in!
#1. Numeric Types and Basic Math in Java
# Basic Math Operations
Java handles addition, subtraction, and multiplication just like your calculator. Remember to use variables to store results.
java
int a = 1 + 2; // a is 3
double b = 1.0 + 3.5; // b is 4.5
int c = 3 - 5; // c is -2
double d = 24 * 1.0; // d is 24.0
- Assignment Operator (=): Evaluates the expression on the right and stores the result in the variable on the left.
- Integer vs. Double:
int
operations withint
result in anint
.- Operations with at least one
double
result in adouble
.
# Incrementing and Decrementing
There are multiple ways to add or subtract 1 from a variable:
- Adding 1:
score = score + 1;
score += 1;
score++;
- Subtracting 1:
score = score - 1;
score -= 1;
score--;
# Modulo Operator (%)
The modulo operator %
gives you the remainder of a division. Only works with integers where the divisor (b) is positive.
java
int a = 4 % 2; // a is 0 (4 = 2 * 2 + 0)
int b = 10 % 3; // b is 1 (10 = 3 * 3 + 1)
int c = 3 % 5; // c is 3 (3 = 0 * 5 + 3)
A smaller number % a larger number always returns the smaller number.
# Division
Division can be tricky! Pay close attention to data types.
java
int a = 5 / 2; // a is 2 (integer division)
double b = 5 / 2.0; // b is 2.5 (double division)
- Integer Division: Returns only the whole number quotient (truncates the decimal).
- Double Division: Returns the actual quotient as a decimal.
#2. Order of Operations
Java follows the standard order of operations (PEMDAS/BODMAS), with modulo included with multiplication and division.
PEMDAS: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right). Modulo (%) has the same precedence as multiplication and division.
#Practice with Order of Operations
Let's break down some examples:
java
int a = 3 + 4 / 3 * (4-1);
double b = (3 * 5 / 2) / (2.0 + 8);
int c = 4 % 3 + 2 * 5 / 4;
int d = (3 * 5 / 2) / (2 + 8);
a = 3 + 4 / 3 * (4 - 1) = 3 + 4 / 3 * 3 = 3 + 1 * 3 = 3 + 3 = 6
b = (3 * 5 / 2) / (2.0 + 8) = (15 / 2) / (10.0) = (7) / (10.0) = 0.7
c = 4 % 3 + 2 * 5 / 4 = 1 + 2 * 5 / 4 = 1 + 10 / 4 = 1 + 2 = 3
d = (3 * 5 / 2) / (2 + 8) = (15 / 2) / (10) = (7) / (10) = 0
Remember integer division! 15 / 2
is 7
, not 7.5
!
#3. Practice Problems
Let's solidify your understanding with some practice problems. These are designed to mimic the types of questions you'll see on the exam.
# Basic Math Practice
What are the values of a, b, and c after the following code executes?
java
int a = 1;
int b = 2;
int c = 3;
a = c;
b = b * 2;
c = 4;
Answer
D. a = 3, b = 4, c = 4What are the values of p, q, and r after the following code executes?
java
int p = 3;
int q = 4;
int r = 5;
p = q;
q = q + 1;
r = r * 2;
Answer
D. p = 4, q = 5, r = 10What are the values of x, y, and z after the following code executes?
java
int x = 2;
int y = 3;
int z = 4;
x = z;
y = y + 2;
z = x * y;
Answer
B. x = 4, y = 5, z = 20What are the values of m, n, and o after the following code executes?
java
int m = 5;
int n = 6;
int o = 7;
m = n;
n = o;
o = m + n;
Answer
A. m = 6, n = 7, o = 13What are the values of x, y, and z after the following code executes?
java
int x = 3;
int y = 4;
int z = 5;
x = x * 2;
y = y + 3;
z = z - 1;
Answer
D. x = 6, y = 7, z = 4What are the values of x, y, and z after the following code executes?
java
int x = 0;
int y = 1;
int z = 2;
x = y - z;
y = x + y;
z = x - y;
Answer
A. x = -1, y = 0, z = -1What are the values of x, y, and z after the following code executes?
java
int x = 3;
int y = 4;
int z = 5;
x = y / x;
y = z / y;
z = x / z;
Answer
B. x = 1, y = 1, z = 0# Modulo Practice
What is the result of 50 % 10?
Answer
C. 0What is the result of 15 % 4?
Answer
B. 3What is the result of 100 % 7?
Answer
C. 2What is the result of 9 % 3?
Answer
A. 0What is the result of 25 % 5?
Answer
A. 0What is the result of 12 % 10?
Answer
B. 2What is the result of 7 % 10?
Answer
A. 7What is the result of 3 % 10?
Answer
C. 3What is the result of 0 % 10?
Answer
B. 0What is the result of 2 % 10?
Answer
A. 2# AP CSA-Style Practice
What is printed when the following code segment is executed?
java
int a = 8;
int b = 3;
double c = 2.0;
System.out.println(7 + a / b * c - 1);
Answer
C. 10.0What is printed when the following code segment is executed?
java
int a = 12;
int b = 4;
double c = 5.0;
System.out.println(3 + a / b * c - 2);
Answer
A. 16.0What is printed when the following code segment is executed?
java
int a = 10;
int b = 5;
double c = 3.0;
System.out.println(2 + a / b * c - 3);
Answer
E. 5.0What is printed when the following code segment is executed?
java
int a = 15;
int b = 6;
double c = 4.0;
System.out.println(1 + a / b * c - 4);
Answer
A. 5.0What is printed when the following code segment is executed?
java
int a = 20;
int b = 8;
double c = 6.0;
System.out.println(6 + a / b * c - 5);
Answer
D. 13.0#Final Exam Focus 🎯
#Key Topics
- Data Types: Know the difference between
int
anddouble
and how they affect operations. - Assignment Operator: Understand how values are assigned and updated.
- Order of Operations: Be precise with PEMDAS and integer division.
- Modulo Operator: Master its use for remainders.
- Integer Division: Remember that
int / int
truncates the decimal.
#
Exam Tips
- Read Carefully: Pay close attention to data types and the order of operations.
- Step-by-Step: Break down complex expressions into smaller steps.
- Integer Division: Watch out for integer division; it's a common pitfall.
- Practice: The more you practice, the more comfortable you'll be.
- Show Your Work: For FRQs, show your steps to get partial credit.
#
Common Mistakes
- Forgetting the order of operations.
- Incorrectly applying integer division.
- Misunderstanding the modulo operator.
- Not tracking variable changes carefully.
#
Practice Question
Practice Questions
Multiple Choice Questions
-
What is the value of
result
after the following code executes?
java int x = 10; double y = 3.0; double result = x / 4 + y * 2; ```
A) 8.0
B) 8.5
C) 9.0
D) 9.5
2. What is the output of the following code?
```
java int a = 7; int b = 3; System.out.println(a % b + a / b); ```
A) 2
B) 3
C) 4
D) 5
Free Response Question
Consider the following class MathFun
:
java
public class MathFun {
private int value;
public MathFun(int num) {
value = num;
}
public void modify() {
value = (value * 2) - (value % 5);
}
public int getValue() {
return value;
}
public static void main(String[] args) {
MathFun obj1 = new MathFun(12);
MathFun obj2 = new MathFun(7);
obj1.modify();
obj2.modify();
System.out.println(obj1.getValue() + " " + obj2.getValue());
}
}
(a) What is the output of the main
method?
(b) Explain in detail how the modify
method changes the value
instance variable.
Scoring Rubric
(a) Output (2 points)
- 1 point for correct output of
obj1
: 24 - 2 = 22 - 1 point for correct output of
obj2
: 14 - 2 = 12 - Correct Output: 22 12
(b) Explanation (3 points)
- 1 point for stating that the
value
is multiplied by 2 - 1 point for stating that the remainder of
value
divided by 5 is calculated. - 1 point for stating that the remainder is subtracted from the doubled
value
.
Explore more resources

How are we doing?
Give us your feedback and let us know how we can improve