Golang Functions Quiz

Golang
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is the basic syntax for declaring a function in Golang?

A
func functionName(parameters) returnType { body }
B
function functionName(parameters) returnType { body }
C
def functionName(parameters) returnType { body }
D
func functionName(parameters): returnType { body }
2

Question 2

What will this simple function call print?

go
package main

import "fmt"

func greet(name string) {
    fmt.Printf("Hello, %s!\n", name)
}

func main() {
    greet("World")
}
A
Hello, World!
B
Hello, name!
C
Compilation error
D
No output
3

Question 3

What is a function signature in Golang?

A
The function name, parameter types, and return types
B
Just the function name
C
The function body
D
The parameter names only
4

Question 4

What does Golang allow for multiple return values?

A
Functions can return multiple values using (type1, type2) syntax
B
Only one return value per function
C
Return values must be in an array
D
Multiple returns require special keywords
5

Question 5

What will this multiple return function output?

go
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)
    }
}
A
Result: 5
B
Error: division by zero
C
Compilation error
D
Result: 0
6

Question 6

What are named return values in Golang?

A
Return parameters with names that can be assigned in the function body
B
Names for the return statement
C
Labels for goto statements
D
Variable names in the calling code
7

Question 7

What will this named return function do?

go
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)
}
A
Sum: 7, Product: 12
B
Sum: 0, Product: 0
C
Compilation error
D
Sum: 3, Product: 4
8

Question 8

What is a variadic function in Golang?

A
A function that accepts a variable number of arguments using ...type syntax
B
A function with variable return types
C
A function that can be called in different ways
D
A recursive function
9

Question 9

What will this variadic function print?

go
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)
}
A
15
B
5
C
Compilation error
D
0
10

Question 10

What is pass-by-value in Golang?

A
Function parameters receive copies of the arguments
B
Parameters receive references to the original values
C
Parameters share memory with the caller
D
Parameters are passed by name
11

Question 11

What will this pass-by-value example show?

go
package main

import "fmt"

func modify(x int) {
    x = 100
}

func main() {
    y := 50
    modify(y)
    fmt.Println(y)
}
A
50
B
100
C
Compilation error
D
0
12

Question 12

How do you modify a slice passed to a function?

A
The slice header is copied, but it points to the same underlying array
B
You cannot modify slices in functions
C
Use pointers to slices
D
Slices are passed by reference
13

Question 13

What will this slice modification function do?

go
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)
}
A
[999 2 3]
B
[1 2 3]
C
Compilation error
D
panic
14

Question 14

What is a function literal in Golang?

A
An anonymous function defined with func() { } syntax
B
A function declared with a name
C
A built-in function
D
A function from the standard library
15

Question 15

What will this function literal example print?

go
package main

import "fmt"

func main() {
    add := func(a, b int) int {
        return a + b
    }
    result := add(3, 4)
    fmt.Println(result)
}
A
7
B
34
C
Compilation error
D
0
16

Question 16

What is a closure in Golang?

A
A function that captures variables from its surrounding scope
B
A function that doesn't take parameters
C
A built-in function
D
A function that returns void
17

Question 17

What will this closure example output?

go
package main

import "fmt"

func main() {
    x := 10
    increment := func() {
        x++
    }
    increment()
    fmt.Println(x)
}
A
11
B
10
C
Compilation error
D
1
18

Question 18

What is the defer statement in Golang?

A
Defers function execution until the surrounding function returns
B
Delays variable initialization
C
Creates asynchronous execution
D
Handles exceptions
19

Question 19

What will this defer example print?

go
package main

import "fmt"

func main() {
    defer fmt.Println("World")
    fmt.Println("Hello")
}
A
Hello\nWorld
B
World\nHello
C
HelloWorld
D
Compilation error
20

Question 20

What is the order of multiple defer statements?

A
Last in, first out (LIFO) - like a stack
B
First in, first out (FIFO)
C
Random order
D
Alphabetical order
21

Question 21

What will this multiple defer example output?

go
package main

import "fmt"

func main() {
    for i := 1; i <= 3; i++ {
        defer fmt.Print(i, " ")
    }
    fmt.Println("Done")
}
A
Done\n3 2 1
B
Done\n1 2 3
C
3 2 1 Done
D
Compilation error
22

Question 22

What is a panic in Golang?

A
A runtime error that stops normal execution and begins panic unwinding
B
A syntax error
C
A compilation error
D
A warning
23

Question 23

What does recover() do in Golang?

A
Stops panic unwinding and returns the panic value
B
Ignores panics
C
Creates panics
D
Handles syntax errors
24

Question 24

What will this panic/recover example do?

go
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")
}
A
Recovered: something went wrong
B
panic: something went wrong
C
Compilation error
D
This won't print
25

Question 25

What is a method in Golang?

A
A function with a receiver parameter that operates on a specific type
B
A special kind of function
C
A built-in function
D
A function without parameters
26

Question 26

What will this method example print?

go
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())
}
A
12
B
7
C
Compilation error
D
0
27

Question 27

What is the difference between value and pointer receivers?

A
Value receivers get a copy, pointer receivers get a reference to the original
B
They are the same
C
Value receivers are faster
D
Pointer receivers don't work
28

Question 28

What will this pointer receiver method do?

go
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)
}
A
1
B
0
C
Compilation error
D
panic
29

Question 29

What is function recursion in Golang?

A
A function that calls itself directly or indirectly
B
A function that calls other functions
C
A built-in function
D
A function with loops
30

Question 30

What will this recursive function compute?

go
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))
}
A
120
B
25
C
Compilation error
D
Infinite recursion
31

Question 31

What is a higher-order function?

A
A function that takes other functions as parameters or returns functions
B
A function with many parameters
C
A built-in function
D
A recursive function
32

Question 32

What will this higher-order function example do?

go
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)
}
A
10
B
5
C
Compilation error
D
25
33

Question 33

What is the blank identifier _ used for in function returns?

A
To ignore unwanted return values from functions
B
To create anonymous functions
C
To declare variables
D
To end function calls
34

Question 34

What will this blank identifier usage show?

go
package main

import "fmt"

func getCoords() (int, int) {
    return 10, 20
}

func main() {
    x, _ := getCoords()
    fmt.Println(x)
}
A
10
B
20
C
Compilation error
D
10 20
35

Question 35

What is the main advantage of Golang's function design?

A
Simple, consistent syntax with powerful features like multiple returns and closures
B
Object-oriented programming only
C
Complex inheritance hierarchies
D
Dynamic typing

QUIZZES IN Golang