Golang Medium-Level Quiz Assessment
This quiz is designed to test your understanding of core and intermediate concepts in Golang. Each question has one correct answer unless stated otherwise and each carries 4 marks. Good luck!
Section 1: Basics and Syntax
What is the output of the following code?
package main import "fmt" func main() { arr := [3]int{1, 2, 3} fmt.Println(len(arr)) }
a) 2
b) 3
c) Compilation error
d) Runtime errorWhich of the following is the correct way to declare and initialize a map in Go?
a) var m map[string]int
b) m := make(map[string]int)
c) m := map[string]int{}
d) All of the aboveWhich keyword is used to handle unexpected runtime conditions in Go?
a) try
b) panic
c) exception
d) recoverWhat is the zero value of a boolean type in Go?
a) true
b) false
c) nil
d) 0
Section 2: Functions and Methods
What is the output of the following code?
package main import "fmt" func greet(name string) string { return "Hello, " + name } func main() { fmt.Println(greet("Alice")) }
a) Hello, Alice
b) Hello Alice
c) greet("Alice")
d) Compilation errorWhich of the following statements is true about functions in Go?
a) Functions cannot return multiple values.
b) Functions can have named return values.
c) Functions must always accept at least one parameter.
d) Go does not support anonymous functions.What is the correct way to declare a variadic function?
a) func sum(nums ...int) int {}
b) func sum(...nums int) int {}
c) func sum(nums []int) int {}
d) func sum(int ...nums) int {}What does the defer keyword do in Go?
a) Executes the function immediately.
b) Defers the execution of a statement until the function exits.
c) Defers the execution until the program exits.
d) Pauses the execution of a statement.
Section 3: Concurrency
Which Go keyword is used to create a new goroutine?
a) thread
b) go
c) goroutine
d) asyncWhat is the purpose of a Go channel?
a) To synchronize goroutines
b) To communicate between goroutines
c) To store data permanently
d) To execute a function concurrentlyWhat happens if a goroutine tries to send data to a nil channel?
a) The program will panic.
b) The goroutine will block indefinitely.
c) The goroutine will terminate gracefully.
d) The data will be discarded.
Section 4: Advanced Topics
Which of the following best describes a Go interface?
a) A blueprint for structs
b) A collection of method signatures
c) A concrete implementation of methods
d) A type that encapsulates multiple data typesWhat is the purpose of the select statement in Go?
a) To choose between multiple case statements in a loop
b) To handle multiple channel operations
c) To select a specific goroutine to execute
d) To manage multiple function calls simultaneouslyWhat is the difference between a buffered and an unbuffered channel in Go?
a) Buffered channels require a buffer function; unbuffered channels do not.
b) Buffered channels block until full; unbuffered channels block until data is received.
c) Buffered channels are synchronous; unbuffered channels are asynchronous.
d) Unbuffered channels block until empty; buffered channels do not block.What is the output of the following code?
package main import "fmt" func main() { ch := make(chan int, 1) ch <- 42 fmt.Println(<-ch) }
a) 42
b) Compilation error
c) Deadlock
d) Runtime error