Bird
Raised Fist0
Goprogramming~10 mins

Switch expression behavior 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 print the correct message based on the value of x.

Go
package main
import "fmt"
func main() {
    x := 2
    switch x {
    case [1]:
        fmt.Println("Two")
    default:
        fmt.Println("Other")
    }
}
Drag options to blanks, or click blank then click option'
A2
B4
C3
D1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Choosing a case value different from x causes the default case to run.
2fill in blank
medium

Complete the switch to print "Even" when n is 2 or 4.

Go
package main
import "fmt"
func main() {
    n := 4
    switch n {
    case 2, [1]:
        fmt.Println("Even")
    default:
        fmt.Println("Odd")
    }
}
Drag options to blanks, or click blank then click option'
A5
B3
C4
D1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Adding odd numbers to the case list causes wrong output.
3fill in blank
hard

Fix the error in the switch expression to correctly print "Hello" when s is "hi".

Go
package main
import "fmt"
func main() {
    s := "hi"
    switch [1] {
    case "hi":
        fmt.Println("Hello")
    default:
        fmt.Println("Bye")
    }
}
Drag options to blanks, or click blank then click option'
Afmt.Println(s)
Bs
C"s"
Ds == "hi"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a string literal or expression instead of the variable causes no matches.
4fill in blank
hard

Fill both blanks to create a switch that prints "Small" for values less than 10 and "Large" otherwise.

Go
package main
import "fmt"
func main() {
    num := 7
    switch {
    case num [1] 10:
        fmt.Println("Small")
    default:
        fmt.Println("Large")
    }
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using > or == causes wrong output.
5fill in blank
hard

Fill all three blanks to create a switch that prints the day type based on the day number (1-7).

Go
package main
import "fmt"
func main() {
    day := 6
    switch day {
    case [1], [2], [3]:
        fmt.Println("Weekend")
    default:
        fmt.Println("Weekday")
    }
}
Drag options to blanks, or click blank then click option'
A6
B7
C1
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Including weekday numbers in the weekend case causes wrong output.

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