Bird
Raised Fist0
Goprogramming~20 mins

Increment and decrement in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Increment & Decrement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of increment in a loop
What is the output of this Go program?
package main
import "fmt"
func main() {
  x := 0
  for i := 0; i < 3; i++ {
    x++
  }
  fmt.Println(x)
}
Go
package main
import "fmt"
func main() {
  x := 0
  for i := 0; i < 3; i++ {
    x++
  }
  fmt.Println(x)
}
A3
B0
C1
DSyntax error
Attempts:
2 left
๐Ÿ’ก Hint
Think about how many times the loop runs and how x changes each time.
โ“ Predict Output
intermediate
2:00remaining
Decrement operator effect
What will this Go program print?
package main
import "fmt"
func main() {
  y := 5
  y--
  fmt.Println(y)
}
Go
package main
import "fmt"
func main() {
  y := 5
  y--
  fmt.Println(y)
}
A5
B6
C4
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Decrement means subtracting one from the variable.
โ“ Predict Output
advanced
2:00remaining
Increment in expression
What is the output of this Go code?
package main
import "fmt"
func main() {
  a := 1
  b := a++
  fmt.Println(a, b)
}
Go
package main
import "fmt"
func main() {
  a := 1
  b := a++
  fmt.Println(a, b)
}
ACompilation error
B1 1
C2 1
D1 2
Attempts:
2 left
๐Ÿ’ก Hint
Go does not support post-increment as an expression.
โ“ Predict Output
advanced
2:00remaining
Decrement in a for loop
What does this Go program print?
package main
import "fmt"
func main() {
  count := 3
  for count > 0 {
    fmt.Print(count, " ")
    count--
  }
}
Go
package main
import "fmt"
func main() {
  count := 3
  for count > 0 {
    fmt.Print(count, " ")
    count--
  }
}
ACompilation error
B3 2 1
C3 3 3
D1 2 3
Attempts:
2 left
๐Ÿ’ก Hint
The loop runs while count is greater than zero and decreases count each time.
๐Ÿง  Conceptual
expert
2:00remaining
Why Go disallows increment in expressions
Why does Go language not allow using increment (++) or decrement (--) operators inside expressions like 'b := a++'?
ABecause Go does not support increment or decrement operators at all.
BBecause Go requires ++ and -- to be used only with pointers.
CBecause Go only allows ++ and -- inside function definitions, not in main.
DBecause ++ and -- are statements, not expressions, to avoid confusion and side effects in expressions.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how Go treats ++ and -- differently from some other languages.

Practice

(1/5)
1. What does the expression i++ do in Go?
easy
A. Decreases the value of i by 1
B. Increases the value of i by 1
C. Multiplies the value of i by 2
D. Divides the value of i by 2

Solution

  1. Step 1: Understand the operator meaning

    The ++ operator in Go adds 1 to the variable it is used on.
  2. Step 2: Apply to variable i

    Using i++ increases the value of i by exactly 1.
  3. Final Answer:

    Increases the value of i by 1 -> Option B
  4. Quick Check:

    i++ means add 1 [OK]
Hint: Remember ++ means add one, -- means subtract one [OK]
Common Mistakes:
  • Confusing ++ with --
  • Thinking ++ multiplies or divides
  • Using ++ as a function
2. Which of the following is the correct syntax to decrease a variable count by 1 in Go?
easy
A. count--
B. --count
C. count++
D. count =- 1

Solution

  1. Step 1: Identify decrement syntax in Go

    In Go, variable-- is the correct way to subtract 1 from a variable.
  2. Step 2: Check each option

    count-- uses count--, which is correct. --count is invalid prefix decrement in Go. count++ increases count. count =- 1 is invalid syntax.
  3. Final Answer:

    count-- -> Option A
  4. Quick Check:

    Use -- to subtract one [OK]
Hint: Use -- to subtract one, not =- [OK]
Common Mistakes:
  • Using --count instead of count--
  • Confusing ++ and --
  • Using count += 1 to decrement
3. What is the output of this Go code?
package main
import "fmt"
func main() {
  x := 5
  x++
  x--
  x++
  fmt.Println(x)
}
medium
A. Syntax error
B. 5
C. 7
D. 6

Solution

  1. Step 1: Trace the increments and decrements

    Start with x = 5. Then x++ makes x = 6. Then x-- makes x = 5 again. Then x++ makes x = 6.
  2. Step 2: Print the final value

    The final value of x is 6, so fmt.Println(x) prints 6.
  3. Final Answer:

    6 -> Option D
  4. Quick Check:

    5 +1 -1 +1 = 6 [OK]
Hint: Add and subtract step by step to find final value [OK]
Common Mistakes:
  • Ignoring one increment or decrement
  • Thinking increments happen before assignment
  • Confusing output with syntax error
4. Find the error in this Go code snippet:
package main
func main() {
  var n int = 10
  n++
  n--
  n = n++
}
medium
A. Assigning n = n++ causes a compile error
B. n-- cannot be used on integers
C. Using n++ as a statement is invalid
D. Variable n must be declared without type

Solution

  1. Step 1: Understand usage of n++ in Go

    In Go, n++ and n-- are statements that increment or decrement n by 1. They cannot be used as expressions.
  2. Step 2: Analyze the assignment n = n++

    Assigning n = n++ tries to use n++ as a value, which is invalid in Go and causes a compile error.
  3. Final Answer:

    Assigning n = n++ causes a compile error -> Option A
  4. Quick Check:

    n++ is a statement, not an expression [OK]
Hint: Remember n++ can't be assigned; it's a statement only [OK]
Common Mistakes:
  • Trying to assign n = n++
  • Thinking n-- is invalid on int
  • Ignoring Go's statement-only rule for ++/--
5. You want to count down from 3 to 1 using a for loop in Go. Which code correctly uses decrement to do this?
for i := 3; i > 0; ??? {
  fmt.Println(i)
}
hard
A. i += 1
B. i++
C. i--
D. --i

Solution

  1. Step 1: Understand the loop goal

    The loop should count down from 3 to 1, so i must decrease each time.
  2. Step 2: Choose the correct decrement expression

    Using i-- decreases i by 1 each loop, matching the goal. Options A, B, and C are wrong: A and B increase i, C is invalid prefix --i in Go.
  3. Final Answer:

    i-- -> Option C
  4. Quick Check:

    Use i-- to count down in loops [OK]
Hint: Use i-- to count down, i++ to count up [OK]
Common Mistakes:
  • Using i++ when counting down
  • Using i += 1 instead of i--
  • Confusing decrement syntax in for loops
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