0
0
Goprogramming~20 mins

Buffered and unbuffered channels in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Channel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of unbuffered channel communication

What is the output of this Go program using an unbuffered channel?

Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan int)
	go func() {
		ch <- 42
	}()
	fmt.Println(<-ch)
}
A42
Bdeadlock
C0
Dcompile error
Attempts:
2 left
💡 Hint

Think about how unbuffered channels block until both sender and receiver are ready.

Predict Output
intermediate
2:00remaining
Output of buffered channel with multiple sends

What will this Go program print?

Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan int, 2)
	ch <- 10
	ch <- 20
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}
A20\n10
B10\n20
Cdeadlock
Dcompile error
Attempts:
2 left
💡 Hint

Buffered channels allow sending without immediate receiving until full.

Predict Output
advanced
2:00remaining
Deadlock detection with unbuffered channel

What happens when this Go program runs?

Go
package main
func main() {
	ch := make(chan int)
	ch <- 1
	<-ch
}
Acompile error
Bprints 1
Ccompiles but no output
Ddeadlock runtime panic
Attempts:
2 left
💡 Hint

Consider that sending on an unbuffered channel blocks until a receiver is ready.

Predict Output
advanced
2:00remaining
Channel capacity and length after sends

What will be the output of this Go program?

Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan int, 3)
	ch <- 5
	ch <- 10
	fmt.Println(len(ch), cap(ch))
}
A2 3
B3 3
C0 3
Dcompile error
Attempts:
2 left
💡 Hint

len returns the number of elements in the channel buffer, cap returns its capacity.

🧠 Conceptual
expert
2:00remaining
Behavior difference between buffered and unbuffered channels

Which statement correctly describes the difference between buffered and unbuffered channels in Go?

AUnbuffered channels store multiple values internally; buffered channels store only one value.
BBuffered channels always block the sender until the receiver receives; unbuffered channels never block.
CUnbuffered channels block the sender until the receiver receives; buffered channels allow sending without blocking until the buffer is full.
DBuffered channels do not support concurrent access; unbuffered channels do.
Attempts:
2 left
💡 Hint

Think about how sending and receiving synchronize in each channel type.