MCQs on Concurrency Basics | Go

Dive into Concurrency Basics in Go with this detailed quiz. Understand the lifecycle of Goroutines, the workings of Channels, and the differences between Buffered and Unbuffered Channels in Go programming.


MCQs on Concurrency Basics in Go

Goroutines and Their Lifecycle

  1. What is a Goroutine in Go?
    a) A function that executes sequentially
    b) A lightweight thread of execution
    c) A type of channel for concurrency
    d) A special type of array
  2. How are Goroutines created in Go?
    a) By using the thread() function
    b) By using the go keyword before a function call
    c) By defining a function inside a Goroutine
    d) By using the start() function
  3. Which statement about Goroutines is true?
    a) They are created by defining a function inside a channel
    b) They are run in parallel on multiple CPUs
    c) They are executed sequentially and not concurrently
    d) They are created by the goRoutine() function
  4. What happens when a Goroutine is launched?
    a) It runs on the main thread
    b) It runs concurrently with other Goroutines
    c) It waits for other Goroutines to finish
    d) It executes synchronously
  5. Which of the following is correct for a Goroutine’s lifecycle?
    a) A Goroutine starts execution once it is declared
    b) A Goroutine must be executed on the same CPU core
    c) A Goroutine is automatically synchronized
    d) A Goroutine terminates once the main function finishes
  6. What is the behavior of a Goroutine once its function completes execution?
    a) It remains active and runs again
    b) It returns to the main function
    c) It terminates and is cleaned up by the Go runtime
    d) It keeps running indefinitely
  7. How does the Go runtime handle the execution of multiple Goroutines?
    a) All Goroutines are executed on a single CPU core
    b) The runtime maps Goroutines to available CPU cores
    c) Goroutines must be manually distributed across cores
    d) Goroutines are queued in a priority list
  8. Can Goroutines share memory in Go?
    a) Yes, but synchronization is required
    b) No, they cannot share memory
    c) Yes, without the need for synchronization
    d) Only Goroutines on the same CPU core can share memory
  9. Which of the following statements about Goroutines is false?
    a) Goroutines are cheaper than threads in terms of memory
    b) Goroutines can run concurrently or in parallel
    c) Goroutines can execute asynchronously with blocking calls
    d) Goroutines cannot share data between them
  10. What will happen if a Goroutine blocks in Go?
    a) The entire program will block
    b) Other Goroutines will continue to run
    c) The Goroutine will be terminated
    d) The program will crash

Channels: Creation, Sending, and Receiving

  1. How do you create a channel in Go?
    a) channel := new(chan int)
    b) channel := make(chan int)
    c) channel := create(chan int)
    d) channel := chan(int)
  2. How do you send a value through a channel in Go?
    a) channel <- value
    b) send(channel, value)
    c) channel.put(value)
    d) channel.push(value)
  3. How do you receive a value from a channel in Go?
    a) value = channel[]
    b) value := channel.receive()
    c) value := <- channel
    d) value = channel.get()
  4. What happens if a channel is empty and you attempt to receive from it?
    a) The program will crash
    b) The operation will return nil
    c) The program will block until a value is sent
    d) It will return the last sent value
  5. What is the behavior when sending a value to a closed channel?
    a) The program will panic
    b) The value is discarded
    c) The send operation will return nil
    d) The value will be added to a queue
  6. How do you close a channel in Go?
    a) close(channel)
    b) channel.close()
    c) channel.close(true)
    d) channel.end()
  7. What is the result of attempting to send a value through a closed channel?
    a) The value is discarded silently
    b) The program panics
    c) The value is added to the channel buffer
    d) The operation returns an error code
  8. What will the following Go code do?
    ch := make(chan int) ch <- 1 close(ch)
    a) Send 1 to the channel and then close it
    b) Block indefinitely
    c) Close the channel without sending any values
    d) Panic because the channel is closed before sending
  9. What happens when you try to receive from a closed channel?
    a) It will block forever
    b) It will return the last value sent
    c) It will return the zero value for the channel type
    d) It will panic
  10. How can you check if a channel is closed in Go?
    a) _, ok := <-channel
    b) channel.isClosed()
    c) ok := channel.closed()
    d) ok := channel.isFinished()

Buffered vs Unbuffered Channels

  1. What is a buffered channel in Go?
    a) A channel with a fixed size that stores values before they are received
    b) A channel that does not store values
    c) A channel that only supports sending
    d) A channel that allows sending and receiving without synchronization
  2. How do you create a buffered channel in Go?
    a) ch := make(chan int, 2)
    b) ch := make(buffered chan int, 2)
    c) ch := new(chan int, 2)
    d) ch := buffer(chan int, 2)
  3. What happens if you send a value to a buffered channel when it is full?
    a) The program will block until space is available
    b) The send operation returns an error
    c) The value will be discarded silently
    d) The program will panic
  4. How does an unbuffered channel differ from a buffered channel?
    a) Unbuffered channels are faster than buffered ones
    b) Unbuffered channels do not store any values and block until both send and receive are ready
    c) Unbuffered channels can store multiple values at once
    d) Unbuffered channels only allow receiving values
  5. Which type of channel requires synchronization for both sending and receiving?
    a) Buffered channel
    b) Unbuffered channel
    c) Both buffered and unbuffered channels
    d) None of the above
  6. What will happen when sending to an unbuffered channel?
    a) It will send the value without waiting for the receiver
    b) It will block until the receiver is ready
    c) It will discard the value if the receiver is not ready
    d) The program will panic
  7. Can you send multiple values to a buffered channel before they are received?
    a) Yes, until the buffer is full
    b) No, buffered channels only accept one value at a time
    c) Yes, but the program will block if the buffer is full
    d) No, buffered channels do not allow sending multiple values
  8. What is the primary advantage of using buffered channels?
    a) They allow for more efficient concurrency
    b) They do not require synchronization
    c) They are faster than unbuffered channels
    d) They eliminate the need for goroutines
  9. How do you know if a buffered channel is full?
    a) By calling channel.isFull()
    b) By checking the number of values in the buffer
    c) By attempting to send a value and checking if it blocks
    d) By checking the length of the channel
  10. Which of the following is a key advantage of unbuffered channels?
    a) They allow for more efficient memory usage
    b) They do not block on sending or receiving
    c) They provide guaranteed delivery of messages
    d) They automatically scale based on load

Answers Table

QnoAnswer
1b) A lightweight thread of execution
2b) By using the go keyword before a function call
3b) They are run in parallel on multiple CPUs
4b) It runs concurrently with other Goroutines
5a) A Goroutine starts execution once it is declared
6c) It terminates and is cleaned up by the Go runtime
7b) The runtime maps Goroutines to available CPU cores
8a) Yes, but synchronization is required
9d) Goroutines cannot share data between them
10b) Other Goroutines will continue to run
11b) channel := make(chan int)
12a) channel <- value
13c) value := <- channel
14c) The program will block until a value is sent
15b) The program will panic
16a) close(channel)
17b) The program panics
18a) Send 1 to the channel and then close it
19c) It will return the zero value for the channel type
20a) _, ok := <-channel
21a) A channel with a fixed size that stores values before they are received
22a) ch := make(chan int, 2)
23a) The program will block until space is available
24b) Unbuffered channels do not store any values and block until both send and receive are ready
25b) Unbuffered channel
26b) It will block until the receiver is ready
27a) Yes, until the buffer is full
28a) They allow for more efficient concurrency
29c) By attempting to send a value and checking if it blocks
30a) They allow for more efficient memory usage

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