AP Computer Science A · Topic 1.3
Expressions and Assignment Statements Practice
Part of Primitive Types.
Practice questions
29
Sample questions
5 of 29 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
int x = 7; int y = 3; System.out.println(x / y);What is printed?
- Acheck_circle
2
- B
2.33
- C
2.0
- D
2.333333
Why
When both operands are int, Java performs integer division and truncates toward zero, so 7 / 3 evaluates to 2.
- A
Sample 2difficulty 2/5
int a = 17; int b = 5; System.out.println(a % b);What is printed?
- Acheck_circle
2
- B
3
- C
3.4
- D
0
Why
17 % 5 returns the remainder of 17 / 5, which is 2 (since 5 * 3 = 15 and 17 - 15 = 2).
- A
Sample 3difficulty 2/5
public static double half(int n) { return n / 2; }What is wrong with this method?
- A
Division by 2 is undefined for negatives
- Bcheck_circle
Integer division truncates; cast at least one operand to double
- C
The return type should be int
- D
Nothing is wrong
Why
n / 2 performs integer division because both operands are int, so half(3) returns 1.0. Use (double) n / 2 or n / 2.0 to retain the fractional part.
- A
Sample 4difficulty 2/5
<code>-3 + 2 * -2</code> evaluates to
- A
−1
- B
1
- Ccheck_circle
−7
- D
−10
Why
2 * −2 = −4; −3 + (−4) = −7.
- A
Sample 5difficulty 2/5
int result = (2 + 3) * 4; System.out.println(result);What is printed?
- Acheck_circle
20
- B
14
- C
24
- D
9
Why
Parentheses force 2 + 3 = 5 to be evaluated first, then 5 * 4 = 20.
- A