Golang Control Flow Quiz
40 comprehensive questions on Golang's control flow statements, covering if-else, switch expressions, for loops, break/continue, and labeled statements — with 20 code examples demonstrating practical usage patterns and edge cases.
Question 1
What is the basic syntax of an if statement in Golang?
Question 2
What will this if statement print?
package main
import "fmt"
func main() {
x := 10
if x > 5 {
fmt.Println("Big")
}
}Question 3
What is an if-else statement used for?
Question 4
What will this if-else chain output?
package main
import "fmt"
func main() {
score := 85
if score >= 90 {
fmt.Println("A")
} else if score >= 80 {
fmt.Println("B")
} else {
fmt.Println("C")
}
}Question 5
What is the purpose of the else clause?
Question 6
What is a switch statement in Golang?
Question 7
What will this switch statement print?
package main
import "fmt"
func main() {
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
default:
fmt.Println("Other")
}
}Question 8
What is the default case in a switch statement?
Question 9
What does fallthrough do in Golang switch?
Question 10
What will this fallthrough example print?
package main
import "fmt"
func main() {
x := 1
switch x {
case 1:
fmt.Print("One ")
fallthrough
case 2:
fmt.Print("Two ")
default:
fmt.Print("Other")
}
}Question 11
What is a type switch in Golang?
Question 12
What will this type switch output?
package main
import "fmt"
func main() {
var x interface{} = 42
switch v := x.(type) {
case int:
fmt.Printf("int: %d\n", v)
case string:
fmt.Printf("string: %s\n", v)
default:
fmt.Println("other")
}
}Question 13
What are the different forms of for loops in Golang?
Question 14
What will this counter-controlled for loop print?
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Print(i, " ")
}
}Question 15
What is a condition-only for loop?
Question 16
What will this condition-only for loop do?
package main
import "fmt"
func main() {
x := 3
for x > 0 {
fmt.Print(x, " ")
x--
}
}Question 17
What is a range-based for loop used for?
Question 18
What will this range loop over a slice print?
package main
import "fmt"
func main() {
nums := []int{10, 20, 30}
for i, v := range nums {
fmt.Printf("%d:%d ", i, v)
}
}Question 19
What does break do in a loop?
Question 20
What will this break example output?
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
if i == 3 {
break
}
fmt.Print(i, " ")
}
}Question 21
What does continue do in a loop?
Question 22
What will this continue example print?
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
if i == 2 {
continue
}
fmt.Print(i, " ")
}
}Question 23
What are labeled statements used for?
Question 24
What will this labeled break do?
package main
import "fmt"
func main() {
outer:
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if i == 1 && j == 1 {
break outer
}
fmt.Printf("%d,%d ", i, j)
}
}
}Question 25
What is an infinite loop in Golang?
Question 26
What will this infinite loop with break print?
package main
import "fmt"
func main() {
count := 0
for {
count++
if count > 3 {
break
}
fmt.Print(count, " ")
}
}Question 27
What is the scope of variables declared in for loops?
Question 28
What will this loop variable scoping example do?
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Print(i, " ")
}
// fmt.Println(i) // This would error
fmt.Println("Done")
}Question 29
What is a blank switch in Golang?
Question 30
What will this blank switch print?
package main
import "fmt"
func main() {
x := 15
switch {
case x < 10:
fmt.Println("Small")
case x < 20:
fmt.Println("Medium")
default:
fmt.Println("Large")
}
}Question 31
What happens when you range over a string?
Question 32
What will this string range loop output?
package main
import "fmt"
func main() {
s := "hello"
for i, r := range s {
fmt.Printf("%d:%c ", i, r)
}
}Question 33
What is the difference between break and continue?
Question 34
What will this nested loop with continue do?
package main
import "fmt"
func main() {
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
if j == 1 {
continue
}
fmt.Printf("%d,%d ", i, j)
}
}
}Question 35
What is a goto statement in Golang?
Question 36
What will this goto example do?
package main
import "fmt"
func main() {
i := 0
loop:
fmt.Print(i, " ")
i++
if i < 3 {
goto loop
}
}Question 37
What is the most common use of if statements?
Question 38
What is the advantage of switch over long if-else chains?
Question 39
What is the most common loop type in Golang?
Question 40
Why are labeled statements rarely used in Golang?
