0
0
Goprogramming~10 mins

Switch expression behavior in Go - Interactive Code Practice

Choose your learning style9 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.