Golang Control Flow Quiz

Golang
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is the basic syntax of an if statement in Golang?

A
if condition { statements }
B
if (condition) { statements }
C
if condition then { statements }
D
condition ? statements
2

Question 2

What will this if statement print?

go
package main

import "fmt"

func main() {
    x := 10
    if x > 5 {
        fmt.Println("Big")
    }
}
A
Big
B
No output
C
Compilation error
D
Small
3

Question 3

What is an if-else statement used for?

A
To execute one block if condition is true, another if false
B
To execute multiple conditions sequentially
C
To create loops
D
To define functions
4

Question 4

What will this if-else chain output?

go
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")
    }
}
A
B
B
A
C
C
D
No output
5

Question 5

What is the purpose of the else clause?

A
To provide a default action when all if/else-if conditions are false
B
To create nested conditions
C
To end the if statement
D
To repeat the condition check
6

Question 6

What is a switch statement in Golang?

A
A multi-way branch that compares a value against multiple cases
B
A loop that repeats until a condition is met
C
A way to define variables
D
A function call
7

Question 7

What will this switch statement print?

go
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")
    }
}
A
Wednesday
B
Other
C
Monday
D
Compilation error
8

Question 8

What is the default case in a switch statement?

A
A case that executes when no other case matches
B
The first case that is checked
C
Required in all switches
D
A special case for errors
9

Question 9

What does fallthrough do in Golang switch?

A
Forces execution to continue to the next case
B
Exits the switch statement
C
Repeats the current case
D
Goes back to the beginning
10

Question 10

What will this fallthrough example print?

go
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")
    }
}
A
One Two
B
One
C
Two
D
Other
11

Question 11

What is a type switch in Golang?

A
A switch that checks the dynamic type of an interface value
B
A switch for primitive types only
C
A switch that changes variable types
D
A switch for string types
12

Question 12

What will this type switch output?

go
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")
    }
}
A
int: 42
B
string: 42
C
other
D
Compilation error
13

Question 13

What are the different forms of for loops in Golang?

A
Counter-controlled, condition-based, and range-based
B
Only counter-controlled like for(i=0; i<n; i++)
C
Only while loops
D
Only infinite loops
14

Question 14

What will this counter-controlled for loop print?

go
package main

import "fmt"

func main() {
    for i := 0; i < 3; i++ {
        fmt.Print(i, " ")
    }
}
A
0 1 2
B
1 2 3
C
0 1 2 3
D
Compilation error
15

Question 15

What is a condition-only for loop?

A
for condition { statements } - equivalent to while loop
B
A loop that runs forever
C
A loop with only initialization
D
A loop that counts
16

Question 16

What will this condition-only for loop do?

go
package main

import "fmt"

func main() {
    x := 3
    for x > 0 {
        fmt.Print(x, " ")
        x--
    }
}
A
3 2 1
B
1 2 3
C
Infinite loop
D
No output
17

Question 17

What is a range-based for loop used for?

A
Iterating over arrays, slices, maps, strings, and channels
B
Creating numeric ranges
C
Repeating a fixed number of times
D
Only for strings
18

Question 18

What will this range loop over a slice print?

go
package main

import "fmt"

func main() {
    nums := []int{10, 20, 30}
    for i, v := range nums {
        fmt.Printf("%d:%d ", i, v)
    }
}
A
0:10 1:20 2:30
B
10 20 30
C
0 1 2
D
Compilation error
19

Question 19

What does break do in a loop?

A
Immediately exits the innermost loop
B
Skips to the next iteration
C
Ends the program
D
Restarts the loop
20

Question 20

What will this break example output?

go
package main

import "fmt"

func main() {
    for i := 0; i < 10; i++ {
        if i == 3 {
            break
        }
        fmt.Print(i, " ")
    }
}
A
0 1 2
B
0 1 2 3
C
3
D
Infinite loop
21

Question 21

What does continue do in a loop?

A
Skips the rest of the current iteration and starts the next one
B
Exits the loop completely
C
Restarts the loop from the beginning
D
Ends the program
22

Question 22

What will this continue example print?

go
package main

import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        if i == 2 {
            continue
        }
        fmt.Print(i, " ")
    }
}
A
0 1 3 4
B
0 1 2 3 4
C
2
D
0 1
23

Question 23

What are labeled statements used for?

A
To break out of or continue outer loops in nested loop structures
B
To name variables
C
To create goto statements
D
To label functions
24

Question 24

What will this labeled break do?

go
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)
        }
    }
}
A
0,0 0,1 0,2 1,0 1,1
B
0,0 0,1 0,2 1,0
C
All combinations
D
Compilation error
25

Question 25

What is an infinite loop in Golang?

A
for { statements } - a loop with no condition
B
A loop that runs forever
C
A loop with condition true
D
All of the above
26

Question 26

What will this infinite loop with break print?

go
package main

import "fmt"

func main() {
    count := 0
    for {
        count++
        if count > 3 {
            break
        }
        fmt.Print(count, " ")
    }
}
A
1 2 3
B
1 2 3 4
C
Infinite output
D
No output
27

Question 27

What is the scope of variables declared in for loops?

A
Variables declared in init are scoped to the loop
B
Variables are global
C
Variables are scoped to the function
D
Variables don't exist outside the loop
28

Question 28

What will this loop variable scoping example do?

go
package main

import "fmt"

func main() {
    for i := 0; i < 3; i++ {
        fmt.Print(i, " ")
    }
    // fmt.Println(i) // This would error
    fmt.Println("Done")
}
A
Print 0 1 2 Done
B
Compilation error
C
Print Done only
D
Runtime error
29

Question 29

What is a blank switch in Golang?

A
switch { cases } - cases contain full conditions
B
A switch with no expression
C
An empty switch
D
All of the above
30

Question 30

What will this blank switch print?

go
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")
    }
}
A
Medium
B
Small
C
Large
D
No output
31

Question 31

What happens when you range over a string?

A
You get index and rune (Unicode code point) for each character
B
You get index and byte for each character
C
You get only the characters
D
Compilation error
32

Question 32

What will this string range loop output?

go
package main

import "fmt"

func main() {
    s := "hello"
    for i, r := range s {
        fmt.Printf("%d:%c ", i, r)
    }
}
A
0:h 1:e 2:l 3:l 4:o
B
h e l l o
C
0 1 2 3 4
D
Compilation error
33

Question 33

What is the difference between break and continue?

A
break exits the loop, continue skips to next iteration
B
They are the same
C
break restarts the loop
D
continue exits the program
34

Question 34

What will this nested loop with continue do?

go
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)
        }
    }
}
A
0,0 0,2 1,0 1,2
B
0,0 0,1 0,2 1,0 1,1 1,2
C
0,0 0,2
D
Compilation error
35

Question 35

What is a goto statement in Golang?

A
An unconditional jump to a labeled statement
B
A function call
C
A loop construct
D
A conditional jump
36

Question 36

What will this goto example do?

go
package main

import "fmt"

func main() {
    i := 0
    loop:
    fmt.Print(i, " ")
    i++
    if i < 3 {
        goto loop
    }
}
A
0 1 2
B
Infinite loop
C
Compilation error
D
No output
37

Question 37

What is the most common use of if statements?

A
Conditional execution based on boolean expressions
B
Looping
C
Function definition
D
Variable declaration
38

Question 38

What is the advantage of switch over long if-else chains?

A
Cleaner syntax and better readability for multiple conditions
B
Faster execution
C
Less memory usage
D
No advantage
39

Question 39

What is the most common loop type in Golang?

A
Range-based for loops for iteration
B
Counter-controlled loops
C
Infinite loops
D
While loops
40

Question 40

Why are labeled statements rarely used in Golang?

A
They can make code harder to follow and maintain
B
They are not supported
C
They are slower
D
They require special syntax

QUIZZES IN Golang