0
0
Goprogramming~20 mins

Return values in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Return Values 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 function with multiple return values?

Consider the following Go code. What will be printed when main runs?

Go
package main
import "fmt"

func divideAndRemainder(a, b int) (int, int) {
    return a / b, a % b
}

func main() {
    q, r := divideAndRemainder(17, 5)
    fmt.Println(q, r)
}
A2 3
B3 3
C3 2
DError: cannot return two values
Attempts:
2 left
💡 Hint

Remember that / is integer division and % is remainder.

Predict Output
intermediate
2:00remaining
What does this Go function return when called?

Look at this Go function and its call. What is the output?

Go
package main
import "fmt"

func greet(name string) string {
    return "Hello, " + name + "!"
}

func main() {
    message := greet("Alice")
    fmt.Println(message)
}
AHello, Alice!
BHello Alice!
CError: missing return statement
Dgreet Alice
Attempts:
2 left
💡 Hint

Check the string concatenation and punctuation.

Predict Output
advanced
2:00remaining
What is the output of this Go function with named return values?

Analyze this Go code using named return values. What will it print?

Go
package main
import "fmt"

func swap(a, b int) (x int, y int) {
    x = b
    y = a
    return
}

func main() {
    a, b := 10, 20
    x, y := swap(a, b)
    fmt.Println(x, y)
}
A20 10
BCompilation error: missing return values
C0 0
D10 20
Attempts:
2 left
💡 Hint

Named return values can be returned by just return without arguments.

Predict Output
advanced
2:00remaining
What error does this Go function produce?

What error will occur when running this Go code?

Go
package main

func add(a, b int) int {
    return a + b
    return a - b
}

func main() {}
ARuntime error: multiple returns
BSyntax error: unreachable code after return
CNo error, returns sum
DCompilation error: missing return statement
Attempts:
2 left
💡 Hint

Check if code after a return statement is allowed.

🧠 Conceptual
expert
2:00remaining
How many values does this Go function return?

Consider this Go function signature. How many values does it return?

func process(data []int) (result int, err error)
AThree values: int, error, and a boolean
BOne value: a struct containing int and error
CNo values, only modifies data in place
DTwo values: an int and an error
Attempts:
2 left
💡 Hint

Look at the parentheses and types in the return signature.