Golang Functions Quiz
35 comprehensive questions on Golang functions, covering declarations, multiple returns, named returns, variadic functions, and pass-by-value semantics — with 18 code examples exploring function design patterns and parameter passing.
Question 1
What is the basic syntax for declaring a function in Golang?
Question 2
What will this simple function call print?
package main
import "fmt"
func greet(name string) {
fmt.Printf("Hello, %s!\n", name)
}
func main() {
greet("World")
}Question 3
What is a function signature in Golang?
Question 4
What does Golang allow for multiple return values?
Question 5
What will this multiple return function output?
package main
import "fmt"
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
}Question 6
What are named return values in Golang?
Question 7
What will this named return function do?
package main
import "fmt"
func calculate(x, y int) (sum int, product int) {
sum = x + y
product = x * y
return
}
func main() {
s, p := calculate(3, 4)
fmt.Printf("Sum: %d, Product: %d\n", s, p)
}Question 8
What is a variadic function in Golang?
Question 9
What will this variadic function print?
package main
import "fmt"
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
result := sum(1, 2, 3, 4, 5)
fmt.Println(result)
}Question 10
What is pass-by-value in Golang?
Question 11
What will this pass-by-value example show?
package main
import "fmt"
func modify(x int) {
x = 100
}
func main() {
y := 50
modify(y)
fmt.Println(y)
}Question 12
How do you modify a slice passed to a function?
Question 13
What will this slice modification function do?
package main
import "fmt"
func modifySlice(s []int) {
if len(s) > 0 {
s[0] = 999
}
}
func main() {
nums := []int{1, 2, 3}
modifySlice(nums)
fmt.Println(nums)
}Question 14
What is a function literal in Golang?
Question 15
What will this function literal example print?
package main
import "fmt"
func main() {
add := func(a, b int) int {
return a + b
}
result := add(3, 4)
fmt.Println(result)
}Question 16
What is a closure in Golang?
Question 17
What will this closure example output?
package main
import "fmt"
func main() {
x := 10
increment := func() {
x++
}
increment()
fmt.Println(x)
}Question 18
What is the defer statement in Golang?
Question 19
What will this defer example print?
package main
import "fmt"
func main() {
defer fmt.Println("World")
fmt.Println("Hello")
}Question 20
What is the order of multiple defer statements?
Question 21
What will this multiple defer example output?
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
defer fmt.Print(i, " ")
}
fmt.Println("Done")
}Question 22
What is a panic in Golang?
Question 23
What does recover() do in Golang?
Question 24
What will this panic/recover example do?
package main
import "fmt"
func mayPanic() {
panic("something went wrong")
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
}()
mayPanic()
fmt.Println("This won't print")
}Question 25
What is a method in Golang?
Question 26
What will this method example print?
package main
import "fmt"
type Rectangle struct {
width, height int
}
func (r Rectangle) area() int {
return r.width * r.height
}
func main() {
rect := Rectangle{3, 4}
fmt.Println(rect.area())
}Question 27
What is the difference between value and pointer receivers?
Question 28
What will this pointer receiver method do?
package main
import "fmt"
type Counter struct {
value int
}
func (c *Counter) increment() {
c.value++
}
func main() {
counter := Counter{0}
counter.increment()
fmt.Println(counter.value)
}Question 29
What is function recursion in Golang?
Question 30
What will this recursive function compute?
package main
import "fmt"
func factorial(n int) int {
if n <= 1 {
return 1
}
return n * factorial(n-1)
}
func main() {
fmt.Println(factorial(5))
}Question 31
What is a higher-order function?
Question 32
What will this higher-order function example do?
package main
import "fmt"
func apply(f func(int) int, x int) int {
return f(x)
}
func double(n int) int {
return n * 2
}
func main() {
result := apply(double, 5)
fmt.Println(result)
}Question 33
What is the blank identifier _ used for in function returns?
Question 34
What will this blank identifier usage show?
package main
import "fmt"
func getCoords() (int, int) {
return 10, 20
}
func main() {
x, _ := getCoords()
fmt.Println(x)
}Question 35
What is the main advantage of Golang's function design?
