Concurrency Quiz - Languages Golang
Free practice quiz: Concurrency (Languages Golang). 52 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages Golang practice quiz has 52 multiple-choice questions on Concurrency, and 52 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 52 questions, freeSample questions
In Go, what is the primary mechanism for achieving concurrency?
Show answer
Answer: B. Goroutines and Channels
Go's concurrency model is built around lightweight, independently executing functions called goroutines, and a powerful communication mechanism called channels, which are used for safe data exchange between goroutines.
When using unbuffered channels in Go, what is the behavior of a send operation?
Show answer
Answer: B. It blocks until a receiving goroutine is ready to receive the value
Unbuffered channels require both the sender and receiver to be ready simultaneously for a communication to occur. This is known as rendezvous.
In Go, what is the `nil` channel behavior when used with `select`?
Show answer
Answer: A. A `select` statement with a `nil` channel case will never be chosen
Receive or send operations on a `nil` channel block forever. Therefore, a `select` statement that includes a case involving a `nil` channel will never proceed on that case if there are other valid (non-`nil`) cases available. If all cases are `nil` or blocked, the `select` will block.
Consider a scenario where multiple goroutines are writing to the same `map[string]int`. What is the most appropriate way to ensure thread safety?
Show answer
Answer: B. Use a `sync.RWMutex` to protect the map.
Maps in Go are not inherently safe for concurrent read/write access. A `sync.RWMutex` is ideal here because it allows multiple readers to access the map concurrently, while writes are exclusive. A regular `sync.Mutex` would also work but would be less performant if reads are frequent.
Which Go package provides synchronization primitives like Mutexes, WaitGroups, and Once?
Show answer
Answer: C. sync
The `sync` package in Go's standard library offers essential primitives for building concurrent programs, including `Mutex`, `RWMutex`, `WaitGroup`, `Once`, `Cond`, and `Pool`.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is a goroutine in Go?
- Which keyword is used to launch a goroutine in Go?
- What is a channel in Go used for?
- What does it mean for a channel to be buffered in Go?
- What is the purpose of `sync.WaitGroup` in Go?
- What is a mutex in Go, and what is its primary purpose?
- In Go, how can you detect if a channel is closed?
- What is the Go keyword used to signal that a goroutine has completed its work on a `sync.WaitGroup`?
- What is a 'select' statement in Go concurrency?
- What is the Go `context` package primarily used for in concurrent programming?
- Consider the following Go code snippet. What is the most likely output if the `wg.Add(1)` is called before `go func() {...}()`?
- What is the purpose of `sync.Mutex` in Go?
- Which of the following correctly describes a potential issue with using `select` in Go when all cases are blocked indefinitely?
- What is the Go construct used for managing a pool of workers that process tasks from a channel?
- What is the `sync.RWMutex` in Go?
- When a channel is closed in Go, what happens when a goroutine attempts to send a value to it?
- What is the purpose of `defer` in Go concurrency?
- What is the Go `sync.Once` type used for?
- What happens if you try to close an already closed channel in Go?
- What is the purpose of `sync.Cond` in Go?
- When a buffered channel in Go is full, what is the behavior of a send operation?
- What is a 'fan-out' pattern in Go concurrency?
- What is a 'fan-in' pattern in Go concurrency?
- What does the `context.WithCancel` function return?
- What is the output of the following Go code and why?
- What is a 'done' channel in Go used for?
- What is the default behavior of a channel send operation if there is no receiver ready?
- Which of the following is NOT a valid way to synchronize goroutines in Go?
- What is the main difference between `sync.Mutex` and `sync.RWMutex`?
- What is the purpose of `sync.Once`'s `Do` method?
- In Go, what is the 'worker pool' concurrency pattern?
- What is the difference between `context.WithCancel`, `context.WithDeadline`, and `context.WithTimeout`?
- What happens when a `select` statement has multiple cases that are ready to proceed simultaneously?
- What is the purpose of a "ping-pong" pattern in Go concurrency?
- Which of the following is true about Go's scheduler?
- What is a common way to signal multiple goroutines to shut down gracefully in Go?
- What does the `context.Context` interface represent?
- What is the purpose of a `select` statement with a `default` case?
- What is the outcome of receiving from a closed, empty buffered channel in Go?
- What is the main concern when multiple goroutines access and modify shared variables without synchronization?
- What is the role of `sync.Map.LoadOrStore`?
- In Go, what is a 'context cancellation signal' used for?
- What is the primary purpose of `sync.Barrier`?
- What is the correct way to create a buffered channel of integers with a capacity of 5 in Go?
- What does the `select` statement guarantee if multiple cases are ready?
- What is the primary function of `sync.Pool.Get()`?
- What is the purpose of `context.WithValue`?