Challenge - 5 Problems
Go Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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") } }
Attempts:
2 left
๐ก Hint
Remember that fallthrough causes the next case to run even if it doesn't match.
โ Incorrect
The switch matches case 2, prints "Two", then fallthrough causes case 3 to run, printing "Three".
โ Predict Output
intermediate2: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") } }
Attempts:
2 left
๐ก Hint
Switch without an expression evaluates cases as booleans in order.
โ Incorrect
The switch has no expression, so it checks each case condition. x == 5 is true, so it prints "Five" and stops.
โ Predict Output
advanced2: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") } }
Attempts:
2 left
๐ก Hint
The variable x is declared in the switch statement and used in case conditions.
โ Incorrect
x is 10, so x > 5 is true, printing "Greater than 5".
โ Predict Output
advanced2: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") } }
Attempts:
2 left
๐ก Hint
Type switches check the dynamic type of an interface value.
โ Incorrect
i holds a float64 value 3.14, so the case float64 matches and prints "float64 3.14".
โ Predict Output
expert2: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") } }
Attempts:
2 left
๐ก Hint
fallthrough passes control to the next case regardless of matching.
โ Incorrect
Case 1 matches, prints "One or Two", fallthrough causes case 3 to run, printing "Three".