Bird
Raised Fist0
Goprogramming~20 mins

Switch expression behavior in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of switch with fallthrough
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 2
    switch x {
    case 1:
        fmt.Println("One")
    case 2:
        fmt.Println("Two")
        fallthrough
    case 3:
        fmt.Println("Three")
    default:
        fmt.Println("Default")
    }
}
ATwo\nThree
BTwo
CTwo\nDefault
DThree
Attempts:
2 left
๐Ÿ’ก Hint
Remember that fallthrough causes the next case to run even if it doesn't match.
โ“ Predict Output
intermediate
2:00remaining
Switch with multiple expressions
What will this Go program print?
Go
package main
import "fmt"
func main() {
    x := 5
    switch {
    case x < 0:
        fmt.Println("Negative")
    case x == 5:
        fmt.Println("Five")
    case x > 0:
        fmt.Println("Positive")
    default:
        fmt.Println("None")
    }
}
AFive
BPositive
CNegative
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Switch without an expression evaluates cases as booleans in order.
โ“ Predict Output
advanced
2:00remaining
Switch with variable initialization and matching
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    switch x := 10; {
    case x < 5:
        fmt.Println("Less than 5")
    case x > 5:
        fmt.Println("Greater than 5")
    default:
        fmt.Println("Equal to 5")
    }
}
ALess than 5
BEqual to 5
CGreater than 5
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
The variable x is declared in the switch statement and used in case conditions.
โ“ Predict Output
advanced
2:00remaining
Switch with type assertion and interface
What will this Go program print?
Go
package main
import "fmt"
func main() {
    var i interface{} = 3.14
    switch v := i.(type) {
    case int:
        fmt.Println("int", v)
    case float64:
        fmt.Println("float64", v)
    default:
        fmt.Println("unknown")
    }
}
ACompilation error
Bint 3
Cunknown
Dfloat64 3.14
Attempts:
2 left
๐Ÿ’ก Hint
Type switches check the dynamic type of an interface value.
โ“ Predict Output
expert
2:00remaining
Switch with multiple expressions and fallthrough
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 1
    switch x {
    case 1, 2:
        fmt.Println("One or Two")
        fallthrough
    case 3:
        fmt.Println("Three")
    case 4:
        fmt.Println("Four")
    default:
        fmt.Println("Default")
    }
}
AOne or Two
BOne or Two\nThree
CThree
DOne or Two\nFour
Attempts:
2 left
๐Ÿ’ก Hint
fallthrough passes control to the next case regardless of matching.

Practice

(1/5)
1. What does a switch statement do in Go?
easy
A. It runs all cases one after another.
B. It chooses one case to run based on a value or condition.
C. It only works with numbers, not strings.
D. It repeats the same case multiple times automatically.

Solution

  1. Step 1: Understand switch purpose

    A switch statement selects one matching case based on a value or condition.
  2. Step 2: Confirm behavior

    It stops after the first matching case, so only one case runs.
  3. Final Answer:

    It chooses one case to run based on a value or condition. -> Option B
  4. Quick Check:

    Switch picks one matching case [OK]
Hint: Switch picks only the first matching case [OK]
Common Mistakes:
  • Thinking switch runs all cases
  • Believing switch only works with numbers
  • Assuming switch repeats cases automatically
2. Which of the following is the correct syntax for a basic switch statement in Go?
easy
A. switch (value) { case 1: doSomething() default: doDefault() }
B. switch value do { case 1: doSomething() default: doDefault() }
C. switch value then { case 1: doSomething() default: doDefault() }
D. switch value { case 1: doSomething() default: doDefault() }

Solution

  1. Step 1: Recall Go switch syntax

    Go uses switch value { case ... } without parentheses or extra keywords.
  2. Step 2: Check options

    switch value { case 1: doSomething() default: doDefault() } matches correct syntax; others add invalid keywords or parentheses.
  3. Final Answer:

    switch value { case 1: doSomething() default: doDefault() } -> Option D
  4. Quick Check:

    Go switch uses braces, no parentheses [OK]
Hint: No parentheses around switch value in Go [OK]
Common Mistakes:
  • Adding parentheses around switch value
  • Using 'then' or 'do' keywords incorrectly
  • Misplacing braces or colons
3. What is the output of this Go code?
package main
import "fmt"
func main() {
  x := 3
  switch x {
  case 1:
    fmt.Println("One")
  case 2, 3:
    fmt.Println("Two or Three")
  default:
    fmt.Println("Other")
  }
}
medium
A. One
B. Other
C. Two or Three
D. No output

Solution

  1. Step 1: Identify switch value and cases

    Variable x is 3. The switch checks cases 1, then 2 or 3, then default.
  2. Step 2: Match case

    Case 2, 3 matches because x is 3, so it prints "Two or Three".
  3. Final Answer:

    Two or Three -> Option C
  4. Quick Check:

    3 matches case 2,3 [OK]
Hint: Check all case values including multiple ones [OK]
Common Mistakes:
  • Ignoring multiple values in one case
  • Choosing default instead of matching case
  • Assuming case 3 is separate from case 2
4. Identify the error in this Go switch code:
package main
import "fmt"
func main() {
  x := 5
  switch {
  case x == 1:
    fmt.Println("One")
  case x == 5:
    fmt.Println("Five")
  case x == 10:
    fmt.Println("Ten")
  }
}
medium
A. No error; code runs and prints "Five"
B. Switch expression is missing a value but cases use conditions
C. Missing default case causes error
D. Case statements must be constants, not conditions

Solution

  1. Step 1: Understand switch without expression

    Switch without a value allows cases to be conditions (boolean expressions).
  2. Step 2: Check cases and output

    x is 5, so case x == 5 is true, printing "Five". No error occurs.
  3. Final Answer:

    No error; code runs and prints "Five" -> Option A
  4. Quick Check:

    Switch without value uses conditions [OK]
Hint: Switch without value uses boolean cases [OK]
Common Mistakes:
  • Thinking default is mandatory
  • Assuming switch must have a value
  • Believing case conditions are invalid
5. Given this Go code, what will be printed?
package main
import "fmt"
func main() {
  score := 75
  switch {
  case score >= 90:
    fmt.Println("Grade A")
  case score >= 80:
    fmt.Println("Grade B")
  case score >= 70:
    fmt.Println("Grade C")
  default:
    fmt.Println("Fail")
  }
}
hard
A. Grade C
B. Grade B
C. Fail
D. Grade A

Solution

  1. Step 1: Analyze switch without expression

    Switch uses conditions in cases, evaluated top to bottom.
  2. Step 2: Evaluate conditions for score = 75

    score >= 90 is false, score >= 80 is false, score >= 70 is true, so prints "Grade C" and stops.
  3. Final Answer:

    Grade C -> Option A
  4. Quick Check:

    First true case runs, here score >= 70 [OK]
Hint: Switch stops at first true condition [OK]
Common Mistakes:
  • Ignoring order of cases
  • Assuming all true cases print
  • Confusing >= with >
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