A common AP CSA mistake is forgetting how Java evaluates expressions, especially when integer division, operator precedence, remainder, and String concatenation appear together.
In this interactive practice set, we cover AP CSA Unit 1.3: Expressions and Output. These 15 multiple-choice questions target integer division, remainder operations, operator precedence, numeric expressions, and String concatenation.
Key Concepts to Remember Before You Start:
-
Integer Division:
When two integers are divided, Java performs integer division and discards
the fractional part. For example,
11 / 4evaluates to2, not2.75. -
Remainder (
%): The remainder operator returns the remainder after integer division. For example,23 % 6evaluates to5. -
Operator Precedence:
Multiplication (
*), division (/), and modulo (%) have higher precedence than addition and subtraction. Operators with the same precedence are evaluated from left to right. -
String Concatenation:
When the
+operator is used with a String, it performs concatenation. Evaluation proceeds according to Java’s normal expression rules. For example,"Age: " + 5 + 2produces"Age: 52".
AP CSA 1.3: Expressions and Output Practice
int b = 5;
System.out.println(a / b);
a and b are integers, Java
performs integer division. 14 / 5 is mathematically
2.8, but the fractional part is discarded, leaving 2.System.out.println(result);
1.
5 * 3 evaluates to 15.2.
8 / 2 evaluates to 4.3. The expression becomes
4 + 15 - 4.4. Evaluating left to right gives
19 - 4 = 15.%) returns the remainder.
6 goes into 23 three times, giving
18. The remainder is 23 - 18 = 5.int y = 10;
System.out.println(x % y);
4 % 10, 10 goes into 4 zero times.
Therefore, the remainder is the original value, 4.First,
12 / 3 evaluates to 4.
Then 4 * 2 evaluates to 8.%) returns the remainder
after integer division. 5 goes into 17
three times, giving 15. The remainder is
17 - 15 = 2.System.out.println(val);
(3 + 2) becomes 5.The expression becomes
20 - 5 * 4 / 2.Multiplication and division are then evaluated from left to right:
5 * 4 = 20, followed by
20 / 2 = 10.Finally, 20 - 10 = 10.+ performs concatenation.
The expression becomes "Result: 3", and the next
+ concatenates the 4, producing
"Result: 34".(3 + 4),
the arithmetic addition is evaluated first, producing
7. The result is then concatenated with the String,
producing Result: 7.int b = 4;
System.out.println(a / b);
a is a double, the expression
performs floating-point division. The result of
15.0 / 4 is 3.75.System.out.println(result);
10 % 4 evaluates to 2.2. The expression becomes
5 + 2 * 2 - 3.3. Multiplication:
2 * 2 = 4.4. The expression becomes
5 + 4 - 3.5. Addition and subtraction are evaluated from left to right:
9 - 3 = 6.First,
5 + 2 is integer addition, producing
7.The next + combines the integer 7
with the String " equals 7", producing
7 equals 7.11 / 2 is evaluated first.Because both operands are integers, integer division produces
5.The result is then concatenated with the String, producing
Answer: 5.0
if and only if the positive integer variable num
is an even number?0.Therefore, num % 2 evaluates to 0
exactly when num is even.int num = 8473;.
Which of the following expressions will correctly extract the
last digit (3) from num?% 10) extracts the rightmost digit
of a positive base-10 integer.The remainder of 8473 / 10 is 3.By contrast, num / 10 using integer division removes
the last digit and produces 847.Unit 1.3 Practice Complete!
AP CSA Practice Navigation
Why Expressions and Operator Precedence Matter for AP CSA
Expression evaluation is an important part of tracing Java code. Integer division, remainder, operator precedence, and String concatenation can change the value produced by an expression.
When tracing AP CSA code, evaluate parentheses first, then apply the appropriate operator precedence and left-to-right evaluation rules. Pay particular attention when integer and floating-point values or Strings appear in the same expression.
Continue Your AP CSA Practice
After practicing expressions and output, continue with the next available practice topic: Topic 1.5, Casting and Range of Variables. Topic 1.4 is part of the current College Board framework but does not yet have a dedicated practice page on JavaTutorOnline.