Bird
Raised Fist0
Goprogramming~10 mins

Assignment operators in Go - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add 5 to variable x using an assignment operator.

Go
package main

import "fmt"

func main() {
    x := 10
    x [1] 5
    fmt.Println(x)
}
Drag options to blanks, or click blank then click option'
A/=
B-=
C*=
D+=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, not add.
2fill in blank
medium

Complete the code to multiply variable y by 3 using an assignment operator.

Go
package main

import "fmt"

func main() {
    y := 4
    y [1] 3
    fmt.Println(y)
}
Drag options to blanks, or click blank then click option'
A/=
B+=
C*=
D-=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+=' will add instead of multiply.
Using '-=' or '/=' will subtract or divide, not multiply.
3fill in blank
hard

Fix the error in the code by choosing the correct assignment operator to subtract 2 from z.

Go
package main

import "fmt"

func main() {
    z := 15
    z [1] 2
    fmt.Println(z)
}
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+=' will add instead of subtract.
Using '*=' or '/=' will multiply or divide, not subtract.
4fill in blank
hard

Fill both blanks to divide variable a by 4 and assign the result back, then print it.

Go
package main

import "fmt"

func main() {
    a := 20
    a [1] 4
    fmt.[2](a)
}
Drag options to blanks, or click blank then click option'
A/=
BPrintln
CPrintf
D+=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+=' instead of '/=' will add instead of divide.
Using Printf without formatting string causes error.
5fill in blank
hard

Fill all three blanks to create a map with keys as uppercase letters and values as their squares, only for numbers greater than 3.

Go
package main

import "fmt"

func main() {
    nums := []int{1, 2, 3, 4, 5}
    squares := map[string]int{}
    for _, n := range nums {
        if n [3] 3 {
            squares[[1]] = [2]
        }
    }
    fmt.Println(squares)
}
Drag options to blanks, or click blank then click option'
Astring(rune(n + 64))
Bn * n
C>
Dn + n
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' instead of '*' for squaring.
Using '<' instead of '>' for filtering.
Not converting number to string for map keys.

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

  1. Step 1: Understand the operator meaning

    The += operator adds the right-hand side value to the left-hand side variable.
  2. Step 2: Understand the assignment effect

    It then stores the sum back into the left-hand side variable, updating its value.
  3. Final Answer:

    Adds and assigns the sum to the left variable -> Option C
  4. 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?
easy
A. x =* 5
B. x = x *=
C. x = *5
D. x *= 5

Solution

  1. Step 1: Identify correct compound assignment syntax

    In Go, the multiplication assignment operator is *=, used as x *= 5.
  2. Step 2: Check each option for syntax correctness

    x *= 5 matches the correct syntax; others are invalid or cause errors.
  3. Final Answer:

    x *= 5 -> Option D
  4. Quick Check:

    Use *= for multiply and assign [OK]
Hint: Remember operator before = for compound assignment [OK]
Common Mistakes:
  • Placing = before * (like =*)
  • Missing space causing syntax errors
  • Using incomplete expressions like x = *5
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
  a := 10
  a += 5
  a -= 3
  fmt.Println(a)
}
medium
A. 12
B. 15
C. 8
D. 10

Solution

  1. Step 1: Calculate after addition

    Starting with a = 10, after a += 5, a becomes 15.
  2. Step 2: Calculate after subtraction

    Then a -= 3 subtracts 3, so a becomes 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    10 + 5 - 3 = 12 [OK]
Hint: Apply operations step-by-step to variable [OK]
Common Mistakes:
  • Adding and subtracting in wrong order
  • Confusing += and -= effects
  • Ignoring initial value of variable
4. Identify the error in this Go code snippet:
package main
import "fmt"
func main() {
  var x int = 10
  x + = 5
  fmt.Println(x)
}
medium
A. The operator + = has a space and is invalid
B. Variable x is not declared
C. Missing semicolon after var declaration
D. fmt package is not imported

Solution

  1. Step 1: Check operator syntax

    The operator += must not have spaces; + = is invalid syntax.
  2. Step 2: Verify other code parts

    Variable x is declared correctly, semicolons are optional in Go, and the fmt package is imported.
  3. Final Answer:

    Operator + = has a space and is invalid -> Option A
  4. Quick Check:

    No spaces in compound operators [OK]
Hint: Compound operators have no spaces (e.g., +=) [OK]
Common Mistakes:
  • Adding spaces inside operators
  • Assuming semicolons are always required
  • Confusing the compound operator with separate + and = operations
5. Given the code below, what will be the final value of total?
package main
import "fmt"
func main() {
  total := 20
  total /= 4
  total *= 3
  total %= 5
  fmt.Println(total)
}
hard
A. 3
B. 0
C. 4
D. 5

Solution

  1. Step 1: Perform division assignment

    total = 20, after total /= 4, total becomes 5 (integer division).
  2. Step 2: Perform multiplication assignment

    Then total *= 3 makes total 15.
  3. Step 3: Perform modulus assignment

    total %= 5 calculates remainder of 15 divided by 5, which is 0.
  4. Step 4: Re-check calculations

    Integer division 20/4 = 5, 5*3=15, 15%5=0.
  5. Final Answer:

    0 -> Option B
  6. Quick Check:

    20/4=5, 5*3=15, 15%5=0 [OK]
Hint: Apply each operator stepwise, watch integer division [OK]
Common Mistakes:
  • Forgetting integer division truncates decimals
  • Mixing up modulus with division
  • Calculating modulus before multiplication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard