The basic assignment operator is =. It assigns the value on the right to the variable on the left.
Click to reveal answer
beginner
What does the operator += do in Java?
The += operator adds the right-hand value to the left-hand variable and then assigns the result back to that variable. For example, x += 5 means x = x + 5.
Click to reveal answer
beginner
Explain the difference between = and == in Java.
= is the assignment operator used to set a value to a variable. == is the equality operator used to compare two values to check if they are equal.
Click to reveal answer
beginner
What does the operator /= do?
The /= operator divides the left-hand variable by the right-hand value and assigns the result back to the variable. For example, x /= 2 means x = x / 2.
Click to reveal answer
beginner
How does the %= operator work in Java?
The %= operator calculates the remainder of dividing the left-hand variable by the right-hand value and assigns it back to the variable. For example, x %= 3 means x = x % 3.
Click to reveal answer
What does the expression x *= 4 do in Java?
AChecks if x equals 4
BMultiplies x by 4 and assigns the result to x
CDivides x by 4 and assigns the result to x
DAssigns 4 to x
✗ Incorrect
x *= 4 means x = x * 4, so it multiplies x by 4 and stores the result in x.
Which operator assigns the remainder of division to a variable?
A/=
B-=
C+=
D%=
✗ Incorrect
The %= operator assigns the remainder of the division back to the variable.
What is the result of int a = 10; a -= 3;?
Aa is 7
Ba is 13
Ca is 3
Da is 10
✗ Incorrect
a -= 3 means a = a - 3, so a becomes 7.
Which operator is used to assign a value to a variable?
A==
B+=
C=
D!=
✗ Incorrect
The = operator assigns the value on the right to the variable on the left.
What does x /= 5 do?
ADivides x by 5 and assigns the result to x
BMultiplies x by 5
CAdds 5 to x
DSubtracts 5 from x
✗ Incorrect
x /= 5 means x = x / 5, dividing x by 5 and storing the result.
Explain how compound assignment operators like += and *= work in Java.
Think about how you can update a variable by combining math and assignment in one step.
You got /3 concepts.
Describe the difference between the assignment operator = and the equality operator ==.
One sets a value, the other checks if two values are the same.
You got /3 concepts.
Practice
(1/5)
1. What does the assignment operator += do in Java?
easy
A. Divides the left variable by the right value
B. Subtracts the right value from the left variable
C. Multiplies the left variable by the right value
D. Adds the right value to the left variable and stores the result in the left variable
Solution
Step 1: Understand the += operator
The += operator adds the right-hand side value to the left-hand side variable.
Step 2: Store the result back in the left variable
The sum replaces the original value in the left variable.
Final Answer:
Adds the right value to the left variable and stores the result in the left variable -> Option D
Quick Check:
x += y means x = x + y [OK]
Hint: Think: add then assign back to the same variable [OK]
Common Mistakes:
Confusing += with subtraction or multiplication
Thinking it only adds without assignment
Mixing up the order of operands
2. Which of the following is the correct syntax to multiply a variable num by 5 and assign the result back to num?
easy
A. num = * 5;
B. num *= 5;
C. num = num *;
D. num = 5 *;
Solution
Step 1: Identify the correct compound assignment syntax
The operator *= multiplies and assigns in one step.
Step 2: Check the variable and value placement
The correct form is num *= 5; which means num = num * 5;.
Final Answer:
num *= 5; -> Option B
Quick Check:
Use *= for multiply and assign [OK]
Hint: Look for operator before equals for combined action [OK]
Common Mistakes:
Placing * after = without variable
Leaving out the variable on right side
Using invalid syntax like num = * 5
3. What is the output of this Java code?
int a = 10; a -= 3; a += 2; System.out.println(a);
medium
A. 9
B. 11
C. 5
D. 15
Solution
Step 1: Calculate a -= 3;
Subtract 3 from 10: 10 - 3 = 7, so a becomes 7.
Step 2: Calculate a += 2;
Add 2 to 7: 7 + 2 = 9, so a becomes 9.
Step 3: Check the printed value
The code prints the current value of a, which is 9.
Final Answer:
9 -> Option A
Quick Check:
10 - 3 + 2 = 9 [OK]
Hint: Perform each assignment step in order carefully [OK]
Common Mistakes:
Adding before subtracting
Mixing up += and -= effects
Printing before assignment changes
4. Find the error in this code snippet:
int x = 5; x + = 3; System.out.println(x);
medium
A. Variable x is not initialized
B. Missing semicolon after System.out.println(x)
C. The operator + = has a space and is invalid
D. The code will print 8 without errors
Solution
Step 1: Check the operator syntax
The operator += must not have spaces; + = is invalid.
Step 2: Confirm other syntax elements
Variable x is initialized and semicolons are correct.
Final Answer:
The operator + = has a space and is invalid -> Option C
Quick Check:
No spaces allowed in compound assignment operators [OK]
Hint: Compound operators have no spaces, write as one token [OK]
Common Mistakes:
Adding spaces inside operators
Assuming code runs despite syntax errors
Confusing initialization errors with operator errors
5. Given the code below, what is the final value of total?
int total = 20; int discount = 5; total -= discount *= 2; System.out.println(total);
hard
A. 10
B. 30
C. 15
D. 5
Solution
Step 1: Evaluate discount *= 2;
This multiplies discount by 2: 5 * 2 = 10, so discount becomes 10.
Step 2: Calculate total -= discount;
Subtract discount (now 10) from total (20): 20 - 10 = 10.
Step 3: Confirm final value
The final value of total is 10, which is printed.
Final Answer:
10 -> Option A
Quick Check:
discount = 5*2=10; total = 20-10=10 [OK]
Hint: Evaluate right side first in compound assignments [OK]
Common Mistakes:
Ignoring that discount changes before subtraction
Subtracting original discount value (5) instead of updated (10)
Confusing order of operations in assignment
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.