Bird
Raised Fist0
Goprogramming~20 mins

Function execution flow in Go - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What happens when a function in Go is called?
easy
A. The code inside the function runs from top to bottom.
B. The program stops and waits for user input.
C. The function immediately returns without running any code.
D. The function runs only if it has no parameters.

Solution

  1. Step 1: Understand function call behavior

    When a function is called, Go executes the code inside it in order, from the first line to the last.
  2. Step 2: Eliminate incorrect options

    Options A, B, and C describe behaviors that are not how Go functions work when called.
  3. Final Answer:

    The code inside the function runs from top to bottom. -> Option A
  4. Quick Check:

    Function execution flow = top to bottom [OK]
Hint: Functions run code line by line when called [OK]
Common Mistakes:
  • Thinking functions run only if no parameters
  • Believing functions wait for user input automatically
  • Assuming functions return immediately without running code
2. Which of the following is the correct way to define a function named greet that returns a string in Go?
easy
A. func greet() { return "Hello" }
B. function greet() { return "Hello" }
C. func greet() string { return "Hello" }
D. func greet() string { print("Hello") }

Solution

  1. Step 1: Check Go function syntax

    In Go, functions start with func, followed by the name, parameters, and return type. func greet() string { return "Hello" } matches this.
  2. Step 2: Verify return statement and type

    func greet() string { return "Hello" } returns a string and has the correct syntax. The version using 'function greet() { return "Hello" }' uses wrong keyword. The version 'func greet() { return "Hello" }' misses return type. The version using print instead of return is incorrect.
  3. Final Answer:

    func greet() string { return "Hello" } -> Option C
  4. Quick Check:

    Correct Go function syntax = func greet() string { return "Hello" } [OK]
Hint: Go functions start with func and specify return type [OK]
Common Mistakes:
  • Using 'function' keyword instead of 'func'
  • Omitting return type when function returns a value
  • Using print instead of return for output
3. What is the output of this Go program?
package main
import "fmt"
func add(a int, b int) int {
    return a + b
}
func main() {
    result := add(3, 4)
    fmt.Println(result)
}
medium
A. 34
B. Compilation error
C. 0
D. 7

Solution

  1. Step 1: Understand function call and return

    The function add takes two integers and returns their sum. Calling add(3, 4) returns 7.
  2. Step 2: Check output statement

    fmt.Println(result) prints the returned value, which is 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add numbers inside function, print returned result [OK]
Common Mistakes:
  • Concatenating numbers as strings (34)
  • Expecting zero without return
  • Thinking code has syntax errors
4. Identify the error in this Go function and select the fix:
func multiply(a int, b int) int {
    product := a * b
}

func main() {
    fmt.Println(multiply(2, 3))
}
medium
A. Change function to void (no return type)
B. Add a return statement: return product
C. Remove parameters a and b
D. Replace * with + operator

Solution

  1. Step 1: Check function return type and body

    The function declares it returns an int but does not have a return statement, causing a compile error.
  2. Step 2: Fix by adding return statement

    Adding return product returns the calculated value, matching the function signature.
  3. Final Answer:

    Add a return statement: return product -> Option B
  4. Quick Check:

    Function must return value if declared [OK]
Hint: Functions with return type must return a value [OK]
Common Mistakes:
  • Forgetting return statement in non-void functions
  • Changing function signature unnecessarily
  • Changing operators without reason
5. Given this Go code, what will be printed?
package main
import "fmt"
func process(x int) int {
    if x > 0 {
        return x * 2
    }
    return 0
}
func main() {
    values := []int{3, -1, 0, 5}
    sum := 0
    for _, v := range values {
        sum += process(v)
    }
    fmt.Println(sum)
}
hard
A. 16
B. 8
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze function process behavior

    For each value, if positive, returns double the value; else returns 0.
  2. Step 2: Calculate sum over values slice

    Values: 3 -> 6, -1 -> 0, 0 -> 0, 5 -> 10; sum = 6 + 0 + 0 + 10 = 16.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    Sum of doubled positives = 16 [OK]
Hint: Sum doubles positives, zeros for others [OK]
Common Mistakes:
  • Adding negative or zero values incorrectly
  • Forgetting return 0 for non-positive
  • Misreading loop or function logic
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