0
0
Goprogramming~20 mins

Function parameters in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Function Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function with multiple parameters
What is the output of this Go program?
Go
package main
import "fmt"
func addMultiply(a int, b int) (int, int) {
    return a + b, a * b
}
func main() {
    sum, product := addMultiply(3, 4)
    fmt.Println(sum, product)
}
A7 7
B12 7
C7 12
D34 12
Attempts:
2 left
💡 Hint
Look at the order of return values and how they are assigned.
Predict Output
intermediate
2:00remaining
Output of function with variadic parameter
What does this Go program print?
Go
package main
import "fmt"
func sumAll(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}
func main() {
    fmt.Println(sumAll(1, 2, 3, 4))
}
A10
B24
CError: variadic parameter not supported
D1234
Attempts:
2 left
💡 Hint
Variadic parameters allow passing any number of arguments.
Predict Output
advanced
2:00remaining
Output of function with pointer parameter
What is printed by this Go program?
Go
package main
import "fmt"
func increment(x *int) {
    *x = *x + 1
}
func main() {
    a := 5
    increment(&a)
    fmt.Println(a)
}
ACompilation error
B5
C0
D6
Attempts:
2 left
💡 Hint
The function changes the value at the pointer's address.
Predict Output
advanced
2:00remaining
Output of function with named return parameters
What does this Go program print?
Go
package main
import "fmt"
func divide(dividend, divisor int) (result int, err string) {
    if divisor == 0 {
        err = "division by zero"
        return
    }
    result = dividend / divisor
    return
}
func main() {
    res, err := divide(10, 0)
    fmt.Println(res, err)
}
A0
B0 division by zero
C10 division by zero
Druntime error: integer divide by zero
Attempts:
2 left
💡 Hint
Named return parameters are zero-valued if not set explicitly.
🧠 Conceptual
expert
2:00remaining
Function parameter passing behavior
Which statement correctly describes how Go passes function parameters?
AGo passes parameters by value, but slices and maps behave like references.
BGo passes parameters by reference, so changes affect the caller's variables.
CGo always passes parameters by value, copying the data.
DGo uses pointers automatically for all parameters to optimize performance.
Attempts:
2 left
💡 Hint
Think about how slices and maps behave differently from basic types.