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