What is the purpose of the assignment operator = in C#?
The assignment operator = is used to store a value into a variable. It takes the value on the right and puts it into the variable on the left.
Click to reveal answer
beginner
What does the compound assignment operator += do?
The += operator adds the value on the right to the variable on the left and then stores the result back into that variable. It's a shortcut for variable = variable + value.
Click to reveal answer
beginner
Explain the difference between = and += operators.
= assigns a new value to a variable, replacing the old one. += adds a value to the current value of the variable and updates it with the sum.
Click to reveal answer
beginner
Given int x = 5; what is the value of x after x *= 3;?
The value of x becomes 15 because x *= 3 multiplies x by 3 and assigns the result back to x.
Click to reveal answer
beginner
Can compound assignment operators be used with strings in C#? For example, str += "hello";
Yes, the += operator can be used to append strings. It adds the right string to the end of the left string and stores the result.
Click to reveal answer
What does the statement count -= 2; do?
ASubtracts 2 from count and updates count
BAssigns 2 to count
CAdds 2 to count
DMultiplies count by 2
✗ Incorrect
The -= operator subtracts the value on the right from the variable on the left and updates it.
Which operator is equivalent to number = number / 4;?
Anumber += 4;
Bnumber /= 4;
Cnumber *= 4;
Dnumber -= 4;
✗ Incorrect
The /= operator divides the variable by the value and assigns the result back.
What is the value of total after total = 10; total += 5;?
A15
B10
C5
D50
✗ Incorrect
The += operator adds 5 to 10, so total becomes 15.
Which of these is NOT a compound assignment operator in C#?
A+=
B-=
C*=
D==
✗ Incorrect
== is a comparison operator, not an assignment operator.
If str = "Hi";, what does str += " there"; do?
AReplaces str with " there"
BDeletes str
CAppends " there" to str
DThrows an error
✗ Incorrect
The += operator appends the string on the right to the existing string.
Explain how compound assignment operators simplify code compared to using regular assignment.
Think about how you can do math and assignment in one step.
You got /3 concepts.
List at least three compound assignment operators in C# and describe what each does.
Remember operators that combine math with assignment.
You got /4 concepts.
Practice
(1/5)
1. What does the compound assignment operator += do in C#?
easy
A. Multiplies the left variable by the right value
B. Subtracts the right value from the left variable
C. Adds the right value to the left variable and assigns the result to the left variable
D. Divides the left variable by the right value
Solution
Step 1: Understand the operator meaning
The += operator adds the right side value to the left side variable.
Step 2: Assign the result back
The result of the addition is stored back into the left variable.
Final Answer:
Adds the right value to the left variable and assigns the result to the left variable -> Option C
Quick Check:
+= means add and assign [OK]
Hint: Remember: += means add then assign in one step [OK]
Common Mistakes:
Confusing += with subtraction or multiplication
Thinking += just adds without assignment
Using += on constants (which is invalid)
2. Which of the following is the correct syntax to multiply a variable x by 5 using a compound assignment in C#?