AP Computer Science A • Unit 1.6

Compound Assignment Operators

Detailed answer explanations for the Unit 1.6 multiple-choice practice questions.

Q1C
Q2D
Q3D
Q4C
Q5B
Q6A
Q7A
Q8D
Q9B
Q10C
Q11B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

int x = 4;
x += 3;
x *= 2;
x--;
System.out.println(x);
A6
B7
C13
D14
Correct answer: C. Applying the three updates in order gives 7, then 14, and the final decrement leaves 13.
Explanation

Each compound assignment uses the current value of x. Adding 3 raises the starting value, multiplying by 2 doubles that result, and x-- decreases the variable by 1. Tracing in order keeps an old value out of a later step.

  • A is not correct: this applies the decrement to 7 but skips the multiplication, and x *= 2 runs first and doubles the stored value.
  • B is not correct: this is the value of x after only the first update, so the trace stops before x *= 2 and x-- have run.
  • D is not correct: this is the value reached after the multiplication, but x-- still runs and decreases the variable by 1 before the value is printed.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

int n = 8;
n /= 3;
n++;
System.out.println(n);
A2
B4
C3.666...
D3
Correct answer: D. Integer division stores 2 in n, and the increment that follows raises the value to 3.
Explanation

A compound assignment operator does not change the normal rules of Java arithmetic. Since n is an int and 3 is an int, 8 / 3 uses integer division and discards the fractional part, giving 2 before n++ runs.

  • A is not correct: this is the value stored by n /= 3, but n++ then increases n by 1 before the value is printed.
  • B is not correct: this would follow from rounding 8 / 3 upward instead of discarding the fractional part, which is not what integer division does.
  • C is not correct: a decimal result is never stored, because n is an int and the fractional part is discarded before the updated value is stored.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

int a = 5;
int b = 2;
a += b * 3;
b += a % 4;
System.out.println(a + " " + b);
A11 2
B15 5
C15 6
D11 5
Correct answer: D. The right-hand side is evaluated first, so a becomes 11 and b then becomes 5.
Explanation

Java evaluates the entire right-hand expression before combining its result with the variable, so trace x op= expression as x = x op (expression). The second update then works from the current value of a rather than from its starting value.

  • A is not correct: the first value is right, but b does not keep its starting value, because b += a % 4 adds the remainder to it.
  • B is not correct: reaching 15 for a would require multiplying the current value of a rather than adding the value of b * 3 to it.
  • C is not correct: reaching 6 for b would require adding 4 itself instead of the remainder produced by a % 4, and the first value is wrong as well.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

int value = 6;
value *= 2 + 3;
System.out.println(value);
A11
B15
C30
DThe code does not compile.
Correct answer: C. Java evaluates the right-hand expression 2 + 3 first, which gives 5, and then computes 6 * 5.
Explanation

When a compound assignment statement is expanded by hand, parentheses belong around the entire right-hand side: x *= a + b becomes x = x * (a + b). The same rule applies here, so the sum is formed before the multiplication takes place.

  • A is not correct: this adds the right-hand result to 6 instead of multiplying by it, but the operator in the statement is *=.
  • B is not correct: reading the statement as value = value * 2 + 3 would give 15, which is the wrong answer because the entire right-hand expression is evaluated first.
  • D is not correct: the statement is valid Java, because the right-hand side of a compound assignment does not have to be a single value; it may be a whole expression.
Q5

Consider the following code segment. What is printed as a result of executing the code segment?

int x = 3;
int y = 4;
x++;
y *= x;
x -= 2;

System.out.println(x + " " + y);
A1 12
B2 16
C4 16
D5 20
Correct answer: B. x is 4 when y *= x runs, giving 16, and the last statement leaves x at 2.
Explanation

When several variables change, each later statement uses their current values. A tracing table records those changes in order and prevents an old value from being carried into the next calculation. Here the increment runs before the multiplication, so the multiplication uses the updated value.

  • A is not correct: this multiplies y by the starting value of x rather than by its current value, and the final subtraction is then applied to that same stale value.
  • C is not correct: the second value is right, but x -= 2 still runs and lowers x from 4 before the printing statement.
  • D is not correct: reaching these values would require x to hold 5 when the multiplication runs, but x++ increases x by 1 only.
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

int value = 23;
value %= 6;
value *= 2;
value--;
System.out.println(value);
A9
B5
C10
D11
Correct answer: A. The remainder step leaves 5, the multiplication gives 10, and value-- lowers the result to 9.
Explanation

The %= operator follows the usual remainder rules for its operand types, so it stores the remainder rather than the result of the division. Each later statement then uses the current value, which is why the trace has to be taken one statement at a time.

  • B is not correct: this is the value stored by the remainder step alone, but two more statements run and change value before it is printed.
  • C is not correct: this is the value after the multiplication, but value-- then decreases the variable by 1 before the printing statement.
  • D is not correct: reaching 11 would require the remainder step to leave a larger value, but value %= 6 stores the remainder of the division and nothing else.
Q7

Which statement can be used to trace total -= 6?

Atotal = total - 6;
Btotal = 6 - total;
Ctotal = total + 6;
Dtotal = total * 6;
Correct answer: A. A compound assignment updates a variable using its current value, so total -= 6 is traced as total = total - 6;.
Explanation

Trace x op= expression as x = x op (expression). Java reads the current value of the variable, combines it with the right-hand value using the matching arithmetic operator, and stores the result back into that same variable.

  • B is not correct: this reverses the operands. Subtraction is not symmetric, so subtracting the variable from 6 is a different calculation from the one the operator performs.
  • C is not correct: this adds instead of subtracting, which is what += does, while -= combines the variable with the right-hand value by subtraction.
  • D is not correct: this multiplies the current value, which is how *= is expanded, but the operator written in the statement is -=.
Q8

Which statement correctly expands x *= a + b?

Ax = x * a + b;
Bx = (x * a) + b;
Cx = a + b;
Dx = x * (a + b);
Correct answer: D. The entire right-hand expression is evaluated first, so a + b has to be placed inside parentheses.
Explanation

Forgetting those parentheses is one of the most common tracing errors on this topic. Java evaluates a + b and only then combines the result with x, which is why x = x * (a + b) is the correct expansion.

  • A is not correct: this multiplies x by a and then adds b, so the variable is combined with only part of the right-hand expression.
  • B is not correct: the added parentheses group x * a, which changes nothing about the order of evaluation, so this carries the same meaning as the previous expansion.
  • C is not correct: this drops the current value of x from the right-hand side, but a compound assignment operator updates a variable using its current value.
Q9

Why does count + 1 not update count?

AThe expression uses an integer literal.
BThe result is not stored in a variable.
CAddition cannot update an int.
DThe variable must appear on the right twice.
Correct answer: B. An expression produces a value where Java expects an expression, but nothing here stores that value back into count.
Explanation

Written by itself as count + 1, the line is not a valid Java statement, so the code does not compile. To update the variable, use count = count + 1, count += 1, or count++.

  • A is not correct: an integer literal is not the problem, since count += 1 uses a literal as well and does update the variable.
  • C is not correct: addition can update an int once the result is stored, as in count = count + 1, so the operation itself is not what fails here.
  • D is not correct: the variable does not have to appear twice on the right, since count += 1 and count++ both update the variable without repeating it.
Q10

Which statement keeps the remainder after dividing value by 5?

Avalue /= 5;
Bvalue *= 5;
Cvalue %= 5;
Dvalue -= 5;
Correct answer: C. The %= operator follows the usual remainder rules, so value %= 5 stores the remainder of the division.
Explanation

Java provides a compound assignment operator for each arithmetic operator, and each one keeps the meaning of that operator. The remainder operator produces what a division leaves over, so %= stores that leftover amount back into the variable.

  • A is not correct: this stores the result of the division rather than the remainder, and for an int the fractional part is discarded instead of being kept.
  • B is not correct: this multiplies the current value by 5, which makes the variable larger rather than reducing it to what a division leaves over.
  • D is not correct: this subtracts 5 once from the current value, which is a single subtraction rather than the remainder left after dividing.
Q11

Which statement can be used to trace score--?

Ascore = score + 1;
Bscore = score - 1;
Cscore -= 2;
Dscore = 1;
Correct answer: B. The decrement operator decreases a variable by 1, so score-- is traced as score = score - 1;.
Explanation

The increment operator ++ increases a variable by 1, and the decrement operator -- decreases a variable by 1. Used as a standalone statement, count-- is traced as count = count - 1.

  • A is not correct: this increases the variable, which is the trace of ++, while -- moves the stored value in the opposite direction.
  • C is not correct: this decreases the variable by 2 rather than by 1, and the decrement operator changes a variable by 1.
  • D is not correct: this replaces the current value with 1 instead of decreasing it, and a compound update keeps the variable's current value in the calculation.