Basics of Golang January 01 ,2025

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

  1. 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 error

  2. Which 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 above

  3. Which keyword is used to handle unexpected runtime conditions in Go?

    a) try
    b) panic
    c) exception
    d) recover

  4. What is the zero value of a boolean type in Go? 

    a) true
    b) false
    c) nil
    d) 0

Section 2: Functions and Methods

  1. 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 error

  2. Which 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.

  3. 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 {}

  4. 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

  1. Which Go keyword is used to create a new goroutine? 

    a) thread
    b) go
    c) goroutine
    d) async

  2. What 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 concurrently

  3. What 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

  1. 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 types

  2. What 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 simultaneously

  3. What 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.

  4. 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

Akshay C
0

You must logged in to post comments.

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech