Go Concurrency MCQ

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

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

Answer:

a) go

Explanation:

The 'go' keyword is used in Go to launch a new goroutine, which is a lightweight thread managed by the Go runtime.

2. What is a goroutine in Go?

a) A Go routine
b) A lightweight thread
c) A function
d) A concurrency library

Answer:

b) A lightweight thread

Explanation:

A goroutine is a lightweight thread of execution managed by the Go runtime, used for handling concurrent tasks.

3. How do you communicate between goroutines in Go?

a) Global variables
b) Channels
c) Direct function calls
d) Shared memory

Answer:

b) Channels

Explanation:

Channels are the preferred way to communicate between goroutines in Go. They provide a way to transfer data between concurrently running goroutines.

4. What is a channel in Go?

a) A type-safe mechanism for function calling
b) A tool for memory management
c) A way to lock resources in concurrent programming
d) A conduit for transmitting data between goroutines

Answer:

d) A conduit for transmitting data between goroutines

Explanation:

A channel in Go is a conduit through which goroutines can communicate with each other, used to send and receive values.

5. How do you create a new channel in Go?

a) new(chan Type)
b) make(chan Type)
c) chan Type{}
d) create(chan Type)

Answer:

b) make(chan Type)

Explanation:

Channels in Go are created using the make function, like 'make(chan Type)'.

6. What is a buffered channel in Go?

a) A channel that stores data in a buffer
b) A faster version of a standard channel
c) A channel that compresses data
d) A type of data structure

Answer:

a) A channel that stores data in a buffer

Explanation:

A buffered channel in Go has a capacity to store a limited number of values without a corresponding receiver for those values.

7. How do you specify a buffered channel in Go?

a) make(chan Type, size)
b) buffered chan Type
c) chan Type{size}
d) make(chan Type) with size

Answer:

a) make(chan Type, size)

Explanation:

Buffered channels in Go are created using the make function with a specified capacity, like 'make(chan Type, size)'.

8. What happens when a goroutine tries to write to a full buffered channel in Go?

a) It fails immediately
b) It blocks until space is available
c) The program terminates
d) It overwrites the oldest data in the channel

Answer:

b) It blocks until space is available

Explanation:

If a goroutine tries to write to a full buffered channel, it will block until another goroutine reads from the channel and frees up space.

9. What is the 'select' statement used for in Go?

a) Selecting a specific goroutine to run
b) Making a choice between different types of channels
c) Waiting on multiple channel operations
d) Choosing between different concurrency patterns

Answer:

c) Waiting on multiple channel operations

Explanation:

The 'select' statement in Go allows a goroutine to wait on multiple communication operations, proceeding with one that is ready to communicate.

10. What does closing a channel signify in Go?

a) The channel is no longer needed
b) No more values will be sent on the channel
c) The channel's memory is being freed
d) The channel has encountered an error

Answer:

b) No more values will be sent on the channel

Explanation:

Closing a channel in Go indicates that no more values will be sent on it. Receivers can still receive data already in the channel.

11. Can a closed channel in Go be reopened?

a) Yes
b) No
c) Only if it was buffered
d) Only within a certain time frame

Answer:

b) No

Explanation:

Once a channel is closed in Go, it cannot be reopened. Attempting to send data on a closed channel will cause a panic.

12. How do you read from a channel until it is closed in Go?

a) Using a for loop with range
b) Polling in a loop
c) Using a select statement
d) With a while loop

Answer:

a) Using a for loop with range

Explanation:

A for loop combined with the range keyword is used to read from a channel until it is closed in Go.

13. What is a deadlock in the context of Go concurrency?

a) When a goroutine is terminated unexpectedly
b) A situation where goroutines are waiting on each other, preventing progress
c) A failure in channel communication
d) An error in the Go runtime

Answer:

b) A situation where goroutines are waiting on each other, preventing progress

Explanation:

A deadlock occurs in Go when goroutines wait on each other to complete, but none of them can proceed, resulting in a standstill.

14. Is it possible to have multiple goroutines receive from the same channel in Go?

a) Yes
b) No
c) Only if the channel is buffered
d) Only if the goroutines are part of the same function

Answer:

a) Yes

Explanation:

In Go, multiple goroutines can receive from the same channel, allowing for concurrent processing of channel data.

15. What does the 'sync' package in Go provide for concurrency control?

a) Channels
b) Goroutines
c) Additional types of loops
d) Concurrency primitives like Mutexes and WaitGroups

Answer:

d) Concurrency primitives like Mutexes and WaitGroups

Explanation:

The 'sync' package in Go provides concurrency primitives such as Mutexes for mutual exclusion and WaitGroups for waiting for a collection of goroutines to finish.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top