0
0
Goprogramming~20 mins

Function execution flow in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Go program?

Consider the following Go code. What will it print when run?

Go
package main
import "fmt"

func foo() int {
    fmt.Println("foo start")
    defer fmt.Println("foo defer")
    fmt.Println("foo end")
    return 1
}

func main() {
    fmt.Println("main start")
    result := foo()
    fmt.Println("result:", result)
    fmt.Println("main end")
}
A
main start
foo start
foo defer
foo end
result: 1
main end
B
main start
foo start
foo end
foo defer
result: 1
main end
C
main start
foo start
foo end
result: 1
foo defer
main end
D
main start
foo start
foo defer
result: 1
foo end
main end
Attempts:
2 left
💡 Hint

Remember that deferred functions run after the surrounding function returns but before it fully exits.

Predict Output
intermediate
2:00remaining
What is the output of this recursive function?

What will this Go program print?

Go
package main
import "fmt"

func countdown(n int) {
    if n == 0 {
        fmt.Println("Blastoff!")
        return
    }
    fmt.Println(n)
    countdown(n - 1)
}

func main() {
    countdown(3)
}
A
3
2
1
0
Blastoff!
B
3
2
1
C
Blastoff!
1
2
3
D
3
2
1
Blastoff!
Attempts:
2 left
💡 Hint

Think about the order of prints and when the function returns.

🔧 Debug
advanced
2:00remaining
Why does this Go program cause a runtime panic?

Examine the code below. Why does it panic at runtime?

Go
package main
import "fmt"

func main() {
    var f func()
    f()
    fmt.Println("Done")
}
ABecause the function variable f is nil and calling it causes a panic.
BBecause the function f is declared but never defined, causing a compile error.
CBecause fmt.Println is called after f(), which is invalid syntax.
DBecause main function must return a value in Go.
Attempts:
2 left
💡 Hint

Think about what happens when you call a function variable that has not been assigned.

📝 Syntax
advanced
2:00remaining
Which option correctly defines and calls an anonymous function in Go?

Which code snippet correctly defines and immediately calls an anonymous function that prints "Hello"?

Afunc() { fmt.Println("Hello") }
Bfunc { fmt.Println("Hello") }()
Cfunc() { fmt.Println("Hello") }()
Dfunc() => { fmt.Println("Hello") }()
Attempts:
2 left
💡 Hint

Anonymous functions need parentheses after their definition to be called immediately.

🚀 Application
expert
2:00remaining
What is the final value of x after this function call sequence?

Given the code below, what is the value of x after modify(&x) is called?

Go
package main
import "fmt"

func modify(p *int) {
    *p += 10
    defer func() {
        *p *= 2
    }()
    *p += 5
}

func main() {
    x := 5
    modify(&x)
    fmt.Println(x)
}
A40
B30
C20
D25
Attempts:
2 left
💡 Hint

Remember that deferred functions run after the surrounding function returns.