Sample questions
Question 1Easy
Go's concurrency motto is:
- Do not communicate by sharing memory; share memory by communicating
- Always use global variables for speed
- Every goroutine needs its own mutex, and all shared variables must be global for speed
- 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
- Go encourages communication through channels instead of shared memory.
- This reduces the need for locks and makes code safer.
- The motto says do not communicate by sharing memory; share memory by communicating.
- This matches option A exactly.
- 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:
- Drops the value if nobody is reading
- Never blocks
- Blocks until a receiver is ready
- 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
- Unbuffered channels require a receiver to be ready before sending.
- Sending blocks until a receiver is available.
- The sender waits until someone reads the value.
- This ensures synchronization between sender and receiver.
- 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))
- 2 2
- 0 2
- 2 0
- 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
- Buffer capacity is 2.
- Two values were sent.
- 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?
- The value is silently dropped
- The send blocks forever
- The program panics
- 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
- Only the sender should close.
- A later send panics.
- 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)
- 5 true 5 true
- 5 true 0 false
- 5 true 0 true
- 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
- The buffered 5 is received first with ok true.
- The channel is now empty and closed.
- 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?
- Use a for loop with a counter
- Set
GOMAXPROCS to 1 - Call
channel.Timeout(d) - 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
- A select with a time.After(d) case waits for d.
- If no other case is ready in time, the time.After case fires.
- 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")
}
}
- Runtime panic
- Deadlock: all goroutines are asleep
- sent b
- 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
- The channel has capacity 1 and already holds 'a'.
- The send of 'b' would block.
- 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 2
- Deadlock: all goroutines are asleep
- 1 2 end
- 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
- A buffered channel of size 3 is created.
- Values 1 and 2 are sent into the channel buffer.
- The channel is closed after the sends.
- The range loop reads 1 from the buffer.
- The range loop reads 2 from the buffer.
- 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")
}
- It prints done
- It prints done after a short wait
- It compiles with an error
- 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
make(chan int) is unbuffered.ch <- 1 waits for a receiver.- 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?
- The first case in source order
- One chosen at random
- All of them, in order
- The last case
Answer: B. One chosen at random
Explanation
Pseudo-random choice prevents starvation of later cases.
Step-by-step solution
- When several select cases are ready, Go picks one at random.
- Source order and position give no priority.
- 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 →