Master goroutine synchronization in Go by exploring essential concepts like mutexes, wait groups, condition variables, using sync.Map for concurrent-safe data, and avoiding race conditions in concurrent programming.
MCQs on Goroutine Synchronization in Go
1. Mutexes, WaitGroups, and Condition Variables
What is the purpose of a mutex in Go? a) To lock a resource to ensure thread-safe access b) To handle errors in goroutines c) To allow goroutines to communicate d) To allocate memory to goroutines
How do you lock a mutex in Go? a) mutex.Lock() b) mutex.Claim() c) mutex.Acquire() d) mutex.Request()
How do you unlock a mutex in Go? a) mutex.Unlock() b) mutex.Release() c) mutex.Free() d) mutex.Close()
What does a sync.WaitGroup do in Go? a) It waits for a group of goroutines to finish execution b) It locks a resource for goroutine synchronization c) It creates a new goroutine d) It handles errors in goroutines
Which method is used to add to the counter of a sync.WaitGroup in Go? a) wg.Add(n) b) wg.Increment(n) c) wg.Set(n) d) wg.Push(n)
How do you wait for all goroutines in a sync.WaitGroup to finish? a) wg.Wait() b) wg.Finish() c) wg.WaitAll() d) wg.Done()
What is a condition variable in Go used for? a) Synchronizing goroutines based on certain conditions b) Managing memory for goroutines c) Scheduling goroutines to run concurrently d) Locking resources for safe access
Which method of a sync.Cond object is used to signal one waiting goroutine? a) cond.Signal() b) cond.Broadcast() c) cond.Notify() d) cond.BroadcastAll()
What does cond.Wait() do in Go? a) It blocks the calling goroutine until it is signaled b) It signals other goroutines to proceed c) It unlocks a mutex d) It finishes the execution of a goroutine
Which method of a sync.Cond object is used to signal all waiting goroutines? a) cond.Broadcast() b) cond.NotifyAll() c) cond.SignalAll() d) cond.BroadcastAll()
2. Using sync.Map for Concurrent-Safe Data
What is sync.Map in Go used for? a) To store data safely in concurrent programs b) To map goroutines to unique IDs c) To track the execution time of goroutines d) To synchronize access to shared resources
Which method of sync.Map is used to store a key-value pair? a) m.Store(key, value) b) m.Set(key, value) c) m.Add(key, value) d) m.Insert(key, value)
How do you retrieve a value from sync.Map by key? a) m.Load(key) b) m.Get(key) c) m.Fetch(key) d) m.Retrieve(key)
What does sync.Map.Load() return? a) The value associated with the key and a boolean indicating existence b) A copy of the map c) The number of entries in the map d) A list of keys in the map
What does sync.Map.Delete() do? a) It removes a key-value pair from the map b) It clears all data in the map c) It locks the map d) It returns an error if the key doesn’t exist
Which method of sync.Map is used to check if a key is present? a) m.Load() b) m.HasKey() c) m.Exists() d) m.Check(key)
How do you iterate over the elements of sync.Map? a) m.Range(func(key, value interface{}) bool {...}) b) m.Iterate(func(key, value interface{}) {...}) c) m.Foreach(func(key, value interface{}) {...}) d) m.Traverse(func(key, value interface{}) {...})
Which of the following is an advantage of using sync.Map? a) It provides concurrent-safe access to data without explicit locking b) It automatically sorts keys in ascending order c) It caches frequently used values d) It handles goroutine errors automatically
When should you prefer sync.Map over a regular map in Go? a) When you need to handle concurrent access to the map b) When you need to store large amounts of data c) When you need to iterate over the map frequently d) When you need to handle errors in the map
Which of the following is NOT a method provided by sync.Map? a) m.Insert() b) m.Store() c) m.Load() d) m.Delete()
3. Avoiding Race Conditions
What is a race condition in concurrent programming? a) A situation where two or more goroutines access shared data simultaneously b) A condition where a goroutine is executed too quickly c) A condition where goroutines are waiting for each other to complete d) A bug that occurs when memory is accessed outside of a goroutine
How can race conditions be prevented in Go? a) Using mutexes to protect shared data b) By using the sync.Map to store data c) By not using goroutines at all d) By avoiding functions that take too long to execute
Which Go tool can help detect race conditions in a program? a) go run -race b) go test -race c) go check -race d) go build -race
What does sync.Mutex protect in Go? a) Shared resources from simultaneous access by multiple goroutines b) Goroutine execution from errors c) Data from being modified d) Data from being written to a file
How do you ensure that data is modified safely by multiple goroutines? a) By using sync.Mutex or sync.RWMutex b) By using a sync.Map only c) By avoiding data sharing between goroutines d) By always using the defer keyword
How does the -race flag help in detecting race conditions? a) It analyzes the code to check for potential data races b) It prevents goroutines from running in parallel c) It locks the data during execution d) It disables all concurrency in the program
Which function in Go is used to signal that a goroutine has completed its work? a) wg.Done() b) wg.Finish() c) wg.End() d) wg.Complete()
How do you ensure safe concurrent modification of a shared map in Go? a) By using a mutex or sync.Map b) By using channels to communicate data c) By not modifying the map concurrently d) By serializing all map modifications
What can happen if race conditions are not properly handled in Go? a) Data corruption and unexpected behavior b) Better performance c) Faster execution time d) More efficient memory usage
What is the purpose of the sync.RWMutex in Go? a) To allow multiple readers but only one writer at a time b) To allow multiple writers at the same time c) To ensure that no goroutines execute concurrently d) To lock a resource in read-only mode
Answers Table
Qno
Answer (Option with Text)
1
a) To lock a resource to ensure thread-safe access
2
a) mutex.Lock()
3
a) mutex.Unlock()
4
a) It waits for a group of goroutines to finish execution
5
a) wg.Add(n)
6
a) wg.Wait()
7
a) Synchronizing goroutines based on certain conditions
8
a) cond.Signal()
9
a) It blocks the calling goroutine until it is signaled
10
a) cond.Broadcast()
11
a) To store data safely in concurrent programs
12
a) m.Store(key, value)
13
a) m.Load(key)
14
a) The value associated with the key and a boolean indicating existence
15
a) It removes a key-value pair from the map
16
a) m.Load()
17
a) m.Range(func(key, value interface{}) bool {...})
18
a) It provides concurrent-safe access to data without explicit locking
19
a) When you need to handle concurrent access to the map
20
a) m.Insert()
21
a) A situation where two or more goroutines access shared data simultaneously
22
a) Using mutexes to protect shared data
23
b) go test -race
24
a) Shared resources from simultaneous access by multiple goroutines
25
a) By using sync.Mutex or sync.RWMutex
26
a) It analyzes the code to check for potential data races
27
a) wg.Done()
28
a) By using a mutex or sync.Map
29
a) Data corruption and unexpected behavior
30
a) To allow multiple readers but only one writer at a time