Golang Online Test - MCQ Questions

Welcome to our Golang Online Test! This test is designed to evaluate your understanding and proficiency in the Go programming language. It consists of 25 multiple-choice questions (MCQs) covering a range of topics from basic syntax and data types to more advanced concepts like concurrency and memory management. Each question includes a coding snippet to provide a practical context for your responses.

The aim of this test is not only to assess your technical knowledge but also to reinforce your learning by explaining the rationale behind each correct answer. This format will help you understand Go’s unique aspects and how to apply them effectively in your coding projects.

Please read each question carefully and select the correct answer. At the end of the test, you will receive feedback on your responses with detailed explanations for each answer. Good luck, and we hope you find this test challenging and informative!

1. What does the following Go snippet print?

func main() {
    var a = []int{1, 2, 3}
    var b = a
    b[0] = 9
    fmt.Println(a)
}
a) [1, 2, 3]
b) [9, 2, 3]
c) [0, 2, 3]
d) nil

2. What will be the output of the following Go code?

func main() {
    x := 5
    y := &x
    *y += 1
    fmt.Println(x)
}
a) 5
b) 6
c) 1
d) Error

3. What does the following Go function return?

func f() int {
    var x int
    defer func() {
        x++
    }()
    return x
}
a) 0
b) 1
c) A runtime error
d) The code does not compile

4. What does the following Go code output?

func main() {
    s := "Go123"
    for _, v := range s {
        fmt.Print(v, " ")
    }
}
a) G o 1 2 3
b) 71 111 49 50 51
c) Go123
d) Error

5. Which of the following is NOT a valid map initialization in Go?

a) m := map[int]string{}
b) m := make(map[int]string)
c) m := new(map[int]string)
d) m := map[int]string{1: "one", 2: "two"}

6. What is the outcome when you run the following Go snippet?

func main() {
    a := []int{1, 2, 3, 4, 5}
    b := a[1:3]
    b = append(b, 30)
    fmt.Println(a)
}
a) [1, 2, 3, 4, 5]
b) [1, 2, 3, 30, 5]
c) [1, 2, 30, 4, 5]
d) [1, 30, 3, 4, 5]

7. Consider the following code, what is the output?

func main() {
    var x float64 = 5.75
    switch {
    case x < 0.0:
        fmt.Println("Negative")
    case x >= 0.0 && x < 6.0:
        fmt.Println("Positive but less than six")
    case x >= 6.0:
        fmt.Println("Positive and six or more")
    }
}
a) Negative
b) Positive but less than six
c) Positive and six or more
d) No output

8. What is the result of the following Go code?

func main() {
    x := map[string]int{"one": 1, "two": 2}
    if val, ok := x["three"]; ok {
        fmt.Println(val)
    } else {
        fmt.Println("Key not found")
    }
}
a) 0
b) 1
c) Key not found
d) nil

9. What error, if any, does the following Go code produce?

import "fmt"

func main() {
    defer fmt.Println("Done")
    panic("An error occurred")
    fmt.Println("This won't print")
}
a) Syntax error
b) Runtime panic
c) Compilation error
d) No error

10. What does this Go code output?

func main() {
    ch := make(chan int)
    go func() {
        ch <- 1
        ch <- 2
        close(ch)
    }()
    for i := range ch {
        fmt.Println(i)
    }
}
a) 1 2
b) 1
c) 1 2 and then a panic
d) Nothing

11. What will this Go function output when called?

func printNumbers() {
    for i := 0; i < 5; i++ {
        defer fmt.Println(i)
    }
}
a) 0 1 2 3 4
b) 4 3 2 1 0
c) 4 4 4 4 4
d) Random numbers

12. In Go, how does the iota keyword function inside a const block?

a) It resets to 0 in each const block.
b) It increments in each line, but resets with a new keyword.
c) It keeps incrementing through multiple const blocks.
d) It generates random numbers.

13. Consider the following Go code. What is the correct way to handle the potential error from WriteString?

import (
    "os"
)
func main() {
    _, err := os.WriteString(os.Stdout, "Hello, world!\n")
    if err != nil {
        log.Fatal(err)
    }
}
a) The code correctly handles the error.
b) The code should check err before writing.
c) log.Fatal should be replaced with fmt.Println for errors.
d) No error handling is needed; WriteString does not produce errors.

14. What is the purpose of the break statement in Go?

a) It terminates the execution of the smallest enclosing loop or switch.
b) It pauses the execution of a loop temporarily.
c) It exits the program immediately.
d) It skips the current iteration of the loop and continues with the next one.

15. What will happen if you try to access an uninitialized map in Go?

func main() {
    var myMap map[string]int
    myMap["key"] = 42
}
a) The program will compile and run without errors.
b) The program will compile but panic at runtime.
c) The program will not compile.
d) The program will ignore the assignment.

16. What will be the output of the following code snippet in Go?

func main() {
    nums := []int{2, 4, 6, 8}
    nums = append(nums, 10, 12)
    fmt.Println(len(nums), cap(nums))
}
a) 6, 8
b) 6, 6
c) 5, 8
d) 6, 12

17. Analyze the following Go code snippet. What will it print?

func main() {
    a := []string{"foo", "bar", "baz"}
    fmt.Println(a[1:2][0])
}
a) foo
b) bar
c) baz
d) [bar]

18. What is the effect of using a goroutine in Go?

a) It causes the program to execute in a single thread.
b) It creates a new thread to run concurrent code.
c) It runs a function concurrently with other functions.
d) It synchronizes multiple functions to run in order.

19. What will be the output of the following Go function?

func main() {
    fmt.Println(strings.Trim("!!Hello, Gophers!!", "!"))
}
a) Hello, Gophers
b) !!Hello, Gophers!!
c) Hello, Gophers!!
d) !!Hello, Gophers

20. Given the following Go code, what will be printed?

import "fmt"

func main() {
    a, b := 10, 20
    swap(&a, &b)
    fmt.Println(a, b)
}

func swap(x, y *int) {
    *x, *y = *y, *x
}
a) 10 20
b) 20 10
c) 0 0
d) The program will not compile

21. What error, if any, will the following Go code produce?

func main() {
    var x int
    defer fmt.Println(x)
    x = 10
}
a) No error, prints 10
b) No error, prints 0
c) Runtime panic
d) Compilation error

22. What does the go keyword do in a Go program?

a) It terminates the current function.
b) It calls a function in a new goroutine.
c) It pauses the execution of the current goroutine.
d) It synchronizes multiple goroutines.

23. How does Go handle memory management?

a) Through manual memory management like C.
b) It uses a garbage collector to automatically free memory.
c) It doesn't manage memory; the programmer must free all allocated memory.
d) It uses reference counting like Python.

24. What is the primary use of the interface{} type in Go?

a) To define a set of methods that must be implemented by a struct.
b) To create generic code that can handle values of any type.
c) To specify that a variable can only hold integer values.
d) To prevent a function from returning any value.

25. What is the significance of the := operator in Go?

a) It declares and initializes a new variable.
b) It is used to check for equality between two values.
c) It is used to append elements to a slice.
d) It defines a constant value.

Comments