Channels & Select Questions with Answers

21 practice questions on Channels & Select (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

Go's concurrency motto is:

  1. Do not communicate by sharing memory; share memory by communicating
  2. Always use global variables for speed
  3. Every goroutine needs its own mutex, and all shared variables must be global for speed
  4. Threads are better than channels

Answer: A. Do not communicate by sharing memory; share memory by communicating

Explanation

Passing data over channels transfers ownership and avoids many locking bugs.

Step-by-step solution

  1. Go encourages communication through channels instead of shared memory.
  2. This reduces the need for locks and makes code safer.
  3. The motto says do not communicate by sharing memory; share memory by communicating.
  4. This matches option A exactly.
  5. Final answer: A.

Why the other options are wrong

  • B. Global variables are not a Go motto; shared state should be guarded or passed through channels.
  • C. Goroutines do not each need their own mutex, and globals are not the way to get speed.
  • D. Go programs use goroutines rather than threads, and channels are the idiomatic way to communicate.

Tip: Remember Go’s motto: communicate via channels, not shared memory.

Question 2Easy

Sending on an unbuffered channel:

  1. Drops the value if nobody is reading
  2. Never blocks
  3. Blocks until a receiver is ready
  4. Returns an error immediately

Answer: C. Blocks until a receiver is ready

Explanation

Unbuffered channels synchronise sender and receiver at the moment of transfer.

Step-by-step solution

  1. Unbuffered channels require a receiver to be ready before sending.
  2. Sending blocks until a receiver is available.
  3. The sender waits until someone reads the value.
  4. This ensures synchronization between sender and receiver.
  5. Final answer: Blocks until a receiver is ready.

Why the other options are wrong

  • A. Sending on an unbuffered channel never drops the value; the send waits until someone reads.
  • B. A send on an unbuffered channel does block, since there is no buffer to hold the value.
  • D. Go does not return an error from a plain send; the sender simply waits for a receiver.

Tip: Remember, unbuffered channels block sending until someone receives.

Question 3Easy

What is printed?

ch := make(chan int, 2)
ch <- 1
ch <- 2
fmt.Println(len(ch), cap(ch))
  1. 2 2
  2. 0 2
  3. 2 0
  4. 1 2

Answer: A. 2 2

Explanation

len of a channel is the number of queued values and cap is the buffer size. Both are 2 here.

Step-by-step solution

  1. Buffer capacity is 2.
  2. Two values were sent.
  3. Both stay queued: len 2.

Why the other options are wrong

  • B. Two values are waiting, so len is not 0.
  • C. The capacity is 2.
  • D. Both sends completed.

Question 4Easy

What happens if a goroutine sends on a closed channel?

  1. The value is silently dropped
  2. The send blocks forever
  3. The program panics
  4. It returns an error value

Answer: C. The program panics

Explanation

Sending on a closed channel causes a run time panic. Receiving from a closed channel is safe and gives zero values.

Step-by-step solution

  1. Only the sender should close.
  2. A later send panics.
  3. Receives still work.

Why the other options are wrong

  • A. Go does not ignore it.
  • B. It does not block; it panics.
  • D. Channel sends have no return value.

Question 5Medium

What is printed?

ch := make(chan int, 1)
ch <- 5
close(ch)
v1, ok1 := <-ch
v2, ok2 := <-ch
fmt.Println(v1, ok1, v2, ok2)
  1. 5 true 5 true
  2. 5 true 0 false
  3. 5 true 0 true
  4. 0 false 0 false

Answer: B. 5 true 0 false

Explanation

A closed channel still delivers buffered values first. After that, receives return the zero value with ok false.

Step-by-step solution

  1. The buffered 5 is received first with ok true.
  2. The channel is now empty and closed.
  3. Next receive gives 0 and false.

Why the other options are wrong

  • A. The value is not repeated.
  • C. ok is false once the channel is drained.
  • D. The buffered value is not lost by close.

Question 6Medium

How is a timeout typically implemented with select?

  1. Use a for loop with a counter
  2. Set GOMAXPROCS to 1
  3. Call channel.Timeout(d)
  4. Add a case <-time.After(d)

Answer: D. Add a case <-time.After(d)

Explanation

time.After returns a channel that receives a value after d, so select picks it if nothing else arrives first.

Step-by-step solution

  1. A select with a time.After(d) case waits for d.
  2. If no other case is ready in time, the time.After case fires.
  3. So the timeout is a case <-time.After(d), which is option D.

Why the other options are wrong

  • A. A loop counter does not stop a channel operation at a time limit.
  • B. GOMAXPROCS sets the number of CPUs used, not a timeout.
  • C. channel.Timeout is not a Go function.

Tip: Use select with case <-time.After(d) to add a timeout.

Question 7Medium

What is the output?

package main

import "fmt"

func main() {
	ch := make(chan string, 1)
	ch <- "a"
	select {
	case ch <- "b":
		fmt.Println("sent b")
	default:
		fmt.Println("full")
	}
}
  1. Runtime panic
  2. Deadlock: all goroutines are asleep
  3. sent b
  4. full

Answer: D. full

Explanation

The buffer already holds one value, so the send is not ready and default runs: a non-blocking send.

Step-by-step solution

  1. The channel has capacity 1 and already holds 'a'.
  2. The send of 'b' would block.
  3. select with default takes default when no case is ready, printing full.

Why the other options are wrong

  • A. No panic occurs.
  • B. default prevents blocking, so no deadlock.
  • C. The send cannot proceed because the buffer is full.

Question 8Medium

What is the output?

package main

import "fmt"

func main() {
	ch := make(chan int, 3)
	ch <- 1
	ch <- 2
	close(ch)
	for v := range ch {
		fmt.Print(v, " ")
	}
	fmt.Println("end")
}
  1. 1 2
  2. Deadlock: all goroutines are asleep
  3. 1 2 end
  4. end

Answer: C. 1 2 end

Explanation

range on a channel drains buffered values and stops when the channel is closed and empty.

Step-by-step solution

  1. A buffered channel of size 3 is created.
  2. Values 1 and 2 are sent into the channel buffer.
  3. The channel is closed after the sends.
  4. The range loop reads 1 from the buffer.
  5. The range loop reads 2 from the buffer.
  6. The loop ends and prints end, giving 1 2 end.

Why the other options are wrong

  • A. 1 2 leaves out end, but Println always runs after the loop finishes.
  • B. The buffer holds two values, and close lets the range loop finish, so no deadlock occurs.
  • D. The values are not lost; the range loop reads both buffered values before it ends.

Tip: Always close a channel to signal the end of data to range loops.

Question 9Hard

What happens when this runs?

func main() {
    ch := make(chan int)
    ch <- 1
    fmt.Println("done")
}
  1. It prints done
  2. It prints done after a short wait
  3. It compiles with an error
  4. Deadlock: fatal error, all goroutines are asleep

Answer: D. Deadlock: fatal error, all goroutines are asleep

Explanation

A send on an unbuffered channel blocks until another goroutine receives. No one will, so the runtime detects the deadlock and aborts.

Step-by-step solution

  1. make(chan int) is unbuffered.
  2. ch <- 1 waits for a receiver.
  3. No other goroutine exists, so deadlock.

Why the other options are wrong

  • A. The send blocks, so done is never printed.
  • B. Nothing ever receives.
  • C. It compiles and runs, then crashes.

Question 10Hard

If several cases in a select are ready at once, which runs?

  1. The first case in source order
  2. One chosen at random
  3. All of them, in order
  4. The last case

Answer: B. One chosen at random

Explanation

Pseudo-random choice prevents starvation of later cases.

Step-by-step solution

  1. When several select cases are ready, Go picks one at random.
  2. Source order and position give no priority.
  3. So a random ready case runs, which is option B.

Why the other options are wrong

  • A. Source order does not decide which ready case runs.
  • C. Select runs only one case per execution.
  • D. The last case has no priority either.

Tip: If you need priority in select, check the important channel first in a separate select.

Practise all 21 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Go topics · All topics