MCQs on Functions | Go

Master the essentials of Functions in Go with this comprehensive quiz. Test your knowledge on defining and calling functions, handling parameters and return values, and understanding anonymous functions and closures in Go programming.


MCQs on Functions in Go

Defining and Calling Functions

  1. How do you define a function in Go?
    a) def functionName() {}
    b) function functionName() {}
    c) func functionName() {}
    d) func functionName() -> {}
  2. Which keyword is used to define a function in Go?
    a) method
    b) func
    c) function
    d) def
  3. What is the correct way to call a function in Go?
    a) call functionName()
    b) functionName()
    c) execute functionName()
    d) invoke functionName()
  4. What will be the output of the following Go code?
    func main() { fmt.Println("Hello World") }
    a) Error: fmt is undefined
    b) Hello World
    c) World Hello
    d) Compilation error
  5. In Go, which part of the function declaration comes after the function name?
    a) Parameters
    b) Return type
    c) Function body
    d) Method
  6. Which is the correct function signature in Go?
    a) func add(a, b int) int {}
    b) func add(int, int) {}
    c) func add(int a, int b) int {}
    d) func add(a, b int) {}
  7. How are parameters defined in a Go function?
    a) Inside parentheses following the function name
    b) Using var before each parameter
    c) By separating them with commas after the function name
    d) Inside square brackets
  8. Which of the following is a valid function declaration in Go?
    a) func sum(a int, b int) -> int {}
    b) func sum(a, b int): int {}
    c) func sum(a int, b int) int {}
    d) func sum(a, b: int) int {}
  9. What is the return type of the following Go function?
    func add(a int, b int) int { return a + b }
    a) int
    b) a, b
    c) void
    d) int, int
  10. Which function signature is incorrect in Go?
    a) func multiply(a int, b int) int
    b) func multiply(int a, int b) int
    c) func multiply(a, b int) int
    d) func multiply(int a, b int) {}

Parameters, Return Values, and Multiple Returns

  1. How do you return multiple values from a function in Go?
    a) By separating the return types with commas
    b) By using an array or slice to return values
    c) By returning a tuple
    d) By using multiple return statements
  2. Which of the following Go functions returns two values?
    func divide(a, b int) (int, int) { return a / b, a % b }
    a) divide
    b) int
    c) a, b
    d) a / b, a % b
  3. What does the following Go function return?
    func swap(a, b int) (int, int) { return b, a }
    a) It returns the sum of a and b
    b) It returns two integers, swapping the values
    c) It returns the difference of a and b
    d) It does not return anything
  4. What is the default return value of a Go function with no return statement?
    a) nil
    b) 0
    c) false
    d) Default values of the return types
  5. What will the following Go function return?
    func getData() (int, string) { return 10, "Go" }
    a) 10
    b) "Go"
    c) (10, "Go")
    d) Error
  6. In Go, if a function returns multiple values, what must be specified when calling the function?
    a) All return values need to be stored in variables
    b) Only one return value can be stored
    c) No variables need to be declared
    d) Only the first return value must be stored
  7. What is the result of calling the following Go function?
    func greet() (string, int) { return "Hello", 10 }
    a) "Hello"
    b) 10
    c) ("Hello", 10)
    d) Error
  8. How do you ignore one of the multiple return values in Go?
    a) By using the _ (underscore) symbol
    b) By using nil
    c) By leaving the second variable blank
    d) By passing false
  9. How can you return different types of values from a Go function?
    a) By declaring a struct
    b) By using pointers
    c) By defining return types explicitly in the function signature
    d) By returning an interface
  10. Which of the following is the correct syntax for multiple return values?
    a) return a, b
    b) return (a, b)
    c) return a and b
    d) return a or b

Anonymous Functions and Closures

  1. What is an anonymous function in Go?
    a) A function without a name that can be called inline
    b) A function defined outside of a class
    c) A function that does not return any value
    d) A function defined in a separate file
  2. How do you define an anonymous function in Go?
    a) func() {}
    b) def() {}
    c) function() {}
    d) func() -> {}
  3. What is the typical use of an anonymous function in Go?
    a) For defining a function with no parameters
    b) For passing a function as an argument to another function
    c) For defining a recursive function
    d) For returning multiple values
  4. Which of the following is an example of a closure in Go?
    func counter() func() int { count := 0 return func() int { count++ return count } }
    a) A function that adds numbers
    b) A function that returns an incrementing counter
    c) A function that performs arithmetic operations
    d) A function that prints data
  5. In Go, closures can access:
    a) Only global variables
    b) Variables declared outside the function
    c) Only function parameters
    d) Static variables
  6. What does the following Go closure do?
    func add(x int) func(int) int { return func(y int) int { return x + y } }
    a) Adds two integers passed to it
    b) Returns a new integer function
    c) Returns the sum of x and y
    d) Adds x to itself
  7. How can you call an anonymous function in Go immediately after defining it?
    a) func() {}()
    b) {}()
    c) function() {}()
    d) call func() {}
  8. What is the result of the following Go code?
    func main() { add := func(x, y int) int { return x + y } fmt.Println(add(5, 6)) }
    a) 5
    b) 11
    c) 5 6
    d) Error
  9. How can a closure be useful in Go?
    a) By defining a global variable that all functions can access
    b) By retaining state between function calls without global variables
    c) By simplifying the code logic
    d) By optimizing the runtime performance
  10. What is the primary feature of a closure in Go?
    a) It cannot modify external variables
    b) It can access and modify variables outside its scope
    c) It always returns values
    d) It is only used for recursion

Answers Table

QnoAnswer
1c) func functionName() {}
2b) func
3b) functionName()
4b) Hello World
5a) Parameters
6a) func add(a int, b int) int {}
7a) Inside parentheses following the function name
8c) func sum(a int, b int) int {}
9a) int
10b) func multiply(int a, int b) int
11c) By returning a tuple
12a) divide
13b) It returns two integers, swapping the values
14d) Default values of the return types
15c) (10, "Go")
16a) All return values need to be stored in variables
17c) ("Hello", 10)
18a) By using the _ (underscore) symbol
19c) By defining return types explicitly in the function signature
20a) return a, b
21a) A function without a name that can be called inline
22a) func() {}
23b) For passing a function as an argument to another function
24b) A function that returns an incrementing counter
25b) Variables declared outside the function
26b) Returns a new integer function
27a) func() {}()
28b) 11
29b) By retaining state between function calls without global variables
30b) It can access and modify variables outside its scope

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top