What is the purpose of assignment operators in Go?
Assignment operators in Go are used to assign values to variables. They can also combine an operation with assignment, like adding a value and then assigning the result.
Click to reveal answer
beginner
What does the operator += do in Go?
The operator += adds the right-hand value to the left-hand variable and then assigns the result back to that variable. For example, x += 3 means x = x + 3.
Click to reveal answer
beginner
Explain the difference between = and *= in Go.
The = operator assigns a value directly to a variable. The *= operator multiplies the variable by a value and assigns the result back to the variable. For example, x *= 2 means x = x * 2.
Click to reveal answer
intermediate
Can you use assignment operators with all data types in Go?
Assignment operators work with numeric types like int, float, and also with strings for the = operator. Operators like += work with numbers and strings (for concatenation). They do not work with all types, like structs or arrays directly.
Click to reveal answer
beginner
What happens if you write x -= 5 in Go?
The value 5 is subtracted from the current value of x, and the result is stored back in x. It is the same as writing x = x - 5.
Click to reveal answer
What does the operator /= do in Go?
ADivides the variable by a value and assigns the result back
BAssigns a new value without any operation
CAdds a value to the variable
DSubtracts a value from the variable
✗ Incorrect
The /= operator divides the variable by the right-hand value and assigns the result back to the variable.
Which operator would you use to add 10 to a variable x in Go?
Ax += 10
Bx =+ 10
Cx =- 10
Dx -= 10
✗ Incorrect
The += operator adds the right-hand value to the variable and assigns the result back.
What is the result of this code snippet?
var x = 5
x *= 3
Ax is 8
Bx is 2
Cx is 15
Dx is 5
✗ Incorrect
The *= operator multiplies x by 3, so x becomes 15.
Which assignment operator can be used to concatenate strings in Go?
A-=
B+=
C*=
D/=
✗ Incorrect
The += operator can concatenate strings by adding the right string to the left.
Is this statement true or false? "The = operator can be used to assign a value to a variable."
AOnly for strings
BFalse
COnly for numbers
DTrue
✗ Incorrect
The = operator assigns any compatible value to a variable.
Explain how assignment operators like += and *= work in Go with an example.
Think about how you can shorten x = x + 3 using an assignment operator.
You got /3 concepts.
List common assignment operators in Go and describe what each does.
Start with simple ones like = and +=, then mention others that combine operations with assignment.
You got /11 concepts.
Practice
(1/5)
1. What does the += operator do in Go?
easy
A. Subtracts the right value from the left variable and stores the result
B. Assigns the right value to the left variable without any operation
C. Adds the right value to the left variable and stores the result in the left variable
D. Multiplies the left variable by the right value and stores the result
Solution
Step 1: Understand the operator meaning
The += operator adds the right-hand side value to the left-hand side variable.
Step 2: Understand the assignment effect
It then stores the sum back into the left-hand side variable, updating its value.
Final Answer:
Adds and assigns the sum to the left variable -> Option C
Quick Check:
+= means add and assign [OK]
Hint: Think: add right value to left and save [OK]
Common Mistakes:
Confusing += with simple assignment =
Thinking it subtracts instead of adds
Mixing it with multiplication or division
2. Which of the following is the correct syntax to multiply variable x by 5 and store the result back in x?