MCQs on Goroutine Synchronization | Go

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

  1. 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
  2. How do you lock a mutex in Go?
    a) mutex.Lock()
    b) mutex.Claim()
    c) mutex.Acquire()
    d) mutex.Request()
  3. How do you unlock a mutex in Go?
    a) mutex.Unlock()
    b) mutex.Release()
    c) mutex.Free()
    d) mutex.Close()
  4. 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
  5. 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)
  6. 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()
  7. 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
  8. 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()
  9. 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
  10. 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

  1. 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
  2. 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)
  3. 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)
  4. 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
  5. 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
  6. 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)
  7. 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{}) {...})
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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()
  8. 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
  9. 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
  10. 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

QnoAnswer (Option with Text)
1a) To lock a resource to ensure thread-safe access
2a) mutex.Lock()
3a) mutex.Unlock()
4a) It waits for a group of goroutines to finish execution
5a) wg.Add(n)
6a) wg.Wait()
7a) Synchronizing goroutines based on certain conditions
8a) cond.Signal()
9a) It blocks the calling goroutine until it is signaled
10a) cond.Broadcast()
11a) To store data safely in concurrent programs
12a) m.Store(key, value)
13a) m.Load(key)
14a) The value associated with the key and a boolean indicating existence
15a) It removes a key-value pair from the map
16a) m.Load()
17a) m.Range(func(key, value interface{}) bool {...})
18a) It provides concurrent-safe access to data without explicit locking
19a) When you need to handle concurrent access to the map
20a) m.Insert()
21a) A situation where two or more goroutines access shared data simultaneously
22a) Using mutexes to protect shared data
23b) go test -race
24a) Shared resources from simultaneous access by multiple goroutines
25a) By using sync.Mutex or sync.RWMutex
26a) It analyzes the code to check for potential data races
27a) wg.Done()
28a) By using a mutex or sync.Map
29a) Data corruption and unexpected behavior
30a) To allow multiple readers but only one writer at a time

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