Advanced Concurrency in Go: Select, Mutex, and WaitGroups Quiz Quiz

Challenge your understanding of Go's concurrency mechanisms including select statements, mutex locks, and wait groups. This quiz covers key concepts, common pitfalls, and practical usage for efficient parallel programming in Go routines.

  1. Select Statement Communication

    Which statement best describes what happens if multiple cases in a Go select statement are ready at the same time?

    1. One ready case is chosen at random for execution.
    2. The select statement blocks until only one case is ready.
    3. All ready cases are executed sequentially.
    4. The program panics due to ambiguity.

    Explanation: When multiple cases in a Go select statement are ready, one of the ready cases is chosen randomly for execution. Contrary to what some assume, Go does not execute all cases or panic in this situation. The statement also does not block if multiple options are ready; it only blocks if none are ready. Choosing one case at random helps prevent starvation.

  2. Mutex Locking Purpose

    What is the main reason for using a Mutex in concurrent Go programs?

    1. To synchronize access to shared data among goroutines.
    2. To launch new goroutines safely.
    3. To prevent goroutines from terminating early.
    4. To increase the speed of program execution.

    Explanation: A Mutex is used in Go to guarantee mutually exclusive access to shared data, preventing race conditions from concurrent writes. While mutexes help avoid data corruption, they do not inherently increase program speed; in fact, improper use can slow a program. Mutexes are unrelated to launching goroutines or preventing their termination; those actions are controlled differently.

  3. WaitGroup Utility

    Why are WaitGroups commonly used when working with goroutines in Go?

    1. To block until a set of goroutines completes their work.
    2. To limit the number of goroutines that can run.
    3. To schedule goroutines at a specific time.
    4. To detect data races among goroutines.

    Explanation: WaitGroups provide a way to wait until a collection of goroutines finishes, allowing the main program to proceed only after all tasks are done. They do not schedule goroutines nor are they a tool for race detection. Limiting the number of running goroutines is better handled by semaphores or buffered channels, not WaitGroups.

  4. Deadlock Scenario

    In which scenario could a Mutex in Go lead to a deadlock?

    1. When multiple goroutines use WaitGroups.
    2. When a goroutine tries to lock a Mutex it already holds without unlocking.
    3. When a select statement is used inside a goroutine.
    4. When a channel is closed properly.

    Explanation: Attempting to lock a Mutex a second time from the same goroutine without unlocking causes a deadlock because the Mutex will never be released. This is a common pitfall when using mutexes. Using WaitGroups or select statements, or properly closing channels, do not inherently cause deadlocks unless misused in combination with other synchronization primitives.

  5. Select Default Case Purpose

    What is the purpose of a default case in a Go select statement?

    1. It increases the priority of a specific channel operation.
    2. It executes only when all cases are ready.
    3. It closes channels automatically when needed.
    4. It allows the select to proceed without blocking if no other case is ready.

    Explanation: A default case in a select statement allows the function to avoid blocking if none of the channels are ready for communication. It does not set channel priorities or close channels, nor does it mean the code waits for all cases to become ready. Therefore, only the first option is correct here.

  6. WaitGroup Misuse Risk

    What issue may arise if WaitGroup.Done is called more times than WaitGroup.Add in a Go program?

    1. Mutexes will be automatically unlocked.
    2. The program will panic at runtime.
    3. No channels can be used in the program.
    4. The goroutines will execute faster.

    Explanation: Calling Done more times than Add results in a negative counter, which causes the WaitGroup to panic at runtime. This is a critical error in goroutine synchronization. The speed of goroutines or use of channels is unaffected, and mutexes are unrelated to this behavior.

  7. Mutex Unlock Practice

    After acquiring a Mutex lock in Go, where is the recommended location to place the Unlock call?

    1. Outside the function where the lock was acquired.
    2. Randomly throughout the function body.
    3. Immediately after the lock, using defer to ensure it's always executed.
    4. After every variable assignment.

    Explanation: The best practice is to place Unlock in a defer statement immediately after acquiring the lock. This ensures that the mutex is always released, even if errors or returns occur. Unlocking randomly, after each assignment, or outside the function is incorrect and may lead to further mistakes or deadlocks.

  8. Select on Closed Channels

    What happens when a Go select statement includes a receive operation on a closed channel?

    1. It blocks forever waiting for data.
    2. It automatically skips that case.
    3. It proceeds with the case and returns the zero value of the channel's type.
    4. The select statement panics.

    Explanation: Receiving from a closed channel returns the zero value for the data type and does not result in blocking. It does not cause a panic when reading, and the case is not skipped if it is ready. Blocking forever only occurs when all channels are open and none are ready, which is not the case with a closed channel.

  9. WaitGroup Counter Adjustment

    Which method is used to increase the counter of a WaitGroup in Go when adding more goroutines to wait for?

    1. WaitGroup.Increment
    2. WaitGroup.Add
    3. WaitGroup.Next
    4. WaitGroup.Up

    Explanation: The Add method increases the internal counter of a WaitGroup to register additional goroutines to wait for. There are no Increment, Next, or Up methods available for WaitGroup in Go, so those are incorrect options.

  10. Select Statement with No Cases

    What is the effect of a select statement in Go with no cases inside it?

    1. It automatically launches a new goroutine.
    2. It logs a warning at runtime.
    3. It blocks forever, causing a deadlock.
    4. It executes instantly without doing anything.

    Explanation: A select with no cases will block forever, effectively creating a deadlock and halting any further execution in that goroutine. It does not log a warning, execute instantly, or launch new goroutines automatically. This pattern is sometimes used deliberately to block but can also be an error.