MCQs on Advanced Concurrency | Go

Explore advanced concurrency concepts in Go, including the use of select statements, channel patterns, worker pools, pipelines, and context for cancellation and deadlines to handle concurrent tasks efficiently.


Select Statements and Channel Patterns

  1. What is the purpose of the select statement in Go?
    a) To handle multiple conditions concurrently
    b) To loop over a range of values
    c) To select a random value from a channel
    d) To return a value from multiple channels
  2. How many cases can a select statement have in Go?
    a) Only one
    b) As many as needed
    c) Two or more
    d) Three or more
  3. What happens when a select statement is blocked?
    a) The program crashes
    b) The select statement moves to the next case
    c) It waits for one of the channels to become ready
    d) It throws an error
  4. Which of the following is NOT valid in a select case?
    a) case <-ch:
    b) case ch := <-ch:
    c) case ch := <- ch:
    d) case x := <-ch:
  5. What is the default keyword used for in a select statement?
    a) It terminates the select block
    b) It ensures that the select statement is non-blocking
    c) It selects the first case automatically
    d) It catches exceptions from channels
  6. What happens when multiple select cases are ready at the same time?
    a) The first case is always executed
    b) The last case is executed
    c) The select statement will randomly choose one case
    d) It will run all cases concurrently
  7. Which of the following is a valid select case with a timeout?
    a) select { case <-ch: time.Sleep(1000): }
    b) select { case <-time.After(1 * time.Second): }
    c) select { case <-ch: time.Wait(1): }
    d) select { case <-ch: timeout(1): }
  8. How can you send a value to a channel within a select statement?
    a) select { case ch <- value: }
    b) select { case value <- ch: }
    c) select { value -> ch: }
    d) select { send ch value: }
  9. What is the use of the select statement with channels?
    a) To control the execution flow of a program
    b) To perform multithreading tasks
    c) To select the first available input or output channel
    d) To synchronize multiple channels
  10. What type of concurrency pattern does the select statement support in Go?
    a) Multiple thread synchronization
    b) Non-blocking concurrent processing
    c) Sequential concurrency
    d) Sequential execution

Worker Pools and Pipelines

  1. In Go, what is the purpose of a worker pool?
    a) To reduce memory usage
    b) To manage concurrency by limiting the number of concurrent goroutines
    c) To share memory among goroutines
    d) To speed up processing of a single task
  2. What is the main advantage of using a worker pool in Go?
    a) More efficient memory usage
    b) Prevents deadlocks
    c) Optimizes concurrent processing
    d) Provides error handling
  3. How are tasks distributed among workers in a worker pool?
    a) Using channels to send tasks to workers
    b) Using global variables to store tasks
    c) Each worker picks tasks randomly
    d) By using the select statement
  4. Which of the following Go constructs are often used in building a worker pool?
    a) Goroutines and channels
    b) Loops and arrays
    c) Functions and maps
    d) Structs and interfaces
  5. In a worker pool, what is the role of a “task queue”?
    a) It stores all the workers
    b) It stores tasks and sends them to workers
    c) It accumulates the results from workers
    d) It defines the worker limits
  6. What type of Go channel is typically used to signal the end of a task in a worker pool?
    a) Buffered channel
    b) Unbuffered channel
    c) Closeable channel
    d) Done channel
  7. In Go, what is a pipeline pattern used for?
    a) To organize concurrency by processing data in stages
    b) To send signals between goroutines
    c) To synchronize multiple threads
    d) To execute long-running computations
  8. Which of the following describes a Go pipeline correctly?
    a) Data flows from one stage to another via channels
    b) Data is written to a global file
    c) Tasks are completed independently without communication
    d) Only one stage can run at a time
  9. What is a common challenge when using a pipeline pattern in Go?
    a) Managing memory for multiple stages
    b) Synchronizing data between stages
    c) Reducing the number of stages
    d) Ensuring one stage runs faster than others
  10. What happens if a channel in a pipeline is closed prematurely?
    a) It causes a panic
    b) It blocks further processing
    c) It allows the pipeline to stop safely
    d) It restarts the entire pipeline

Context for Cancellation and Deadline

  1. What is the purpose of the context package in Go?
    a) To manage global variables
    b) To handle cancellation signals and deadlines in concurrent tasks
    c) To manage goroutines in a cluster
    d) To store values during concurrent operations
  2. How is a context typically passed in Go?
    a) By passing a reference to the context object
    b) By passing a string representing the context
    c) By passing it as the first argument to a function
    d) By using global variables
  3. What is the function used to create a new context with a deadline?
    a) context.WithTimeout()
    b) context.WithDeadline()
    c) context.New()
    d) context.Create()
  4. What does the context.CancelFunc function do?
    a) It allows tasks to continue executing after a timeout
    b) It schedules new tasks for execution
    c) It cancels a context and all associated goroutines
    d) It extends the deadline of a context
  5. What happens if a goroutine is passed a context that has been canceled?
    a) The goroutine completes without issues
    b) The goroutine immediately stops
    c) The goroutine pauses
    d) The goroutine throws an error
  6. Which of the following functions is used to retrieve the cancellation status from a context?
    a) context.IsCanceled()
    b) context.Done()
    c) context.Check()
    d) context.Status()
  7. What happens when a context is passed with a deadline that has expired?
    a) The task continues normally
    b) The task is canceled or returns an error
    c) The task retries automatically
    d) The context extends the deadline
  8. How do you pass a context through channels in Go?
    a) By passing the context object directly
    b) By passing a pointer to the context object
    c) By passing the context’s cancellation signal
    d) By storing the context in a global variable
  9. What is the recommended approach for dealing with a canceled context in Go?
    a) Ignore the cancellation signal
    b) Always check for cancellation and handle it appropriately
    c) Throw an exception
    d) Automatically restart the task
  10. What is the function used to create a context with a cancellation signal?
    a) context.WithCancel()
    b) context.NewCancel()
    c) context.CancelSignal()
    d) context.Cancel()

Answer Key

QNoAnswer (Option with text)
1a) To handle multiple conditions concurrently
2b) As many as needed
3c) It waits for one of the channels to become ready
4c) case ch := <- ch:
5b) It ensures that the select statement is non-blocking
6c) The select statement will randomly choose one case
7b) select { case <-time.After(1 * time.Second): }
8a) select { case ch <- value: }
9c) To select the first available input or output channel
10b) Non-blocking concurrent processing
11b) To manage concurrency by limiting the number of concurrent goroutines
12c) Optimizes concurrent processing
13a) Using channels to send tasks to workers
14a) Goroutines and channels
15b) It stores tasks and sends them to workers
16d) Done channel
17a) To organize concurrency by processing data in stages
18a) Data flows from one stage to another via channels
19b) Synchronizing data between stages
20c) It allows the pipeline to stop safely
21b) To handle cancellation signals and deadlines in concurrent tasks
22c) By passing it as the first argument to a function
23b) context.WithDeadline()
24c) It cancels a context and all associated goroutines
25b) The goroutine immediately stops
26b) context.Done()
27b) The task is canceled or returns an error
28a) By passing the context object directly
29b) Always check for cancellation and handle it appropriately
30a) context.WithCancel()

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