0
0
Goprogramming~20 mins

Default case in select in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of select with default case
What is the output of this Go program?
Go
package main
import (
	"fmt"
	"time"
)
func main() {
	ch := make(chan int)
	select {
	case val := <-ch:
		fmt.Println("Received", val)
	default:
		fmt.Println("No value received")
	}
}
ANo value received
BReceived 0
CProgram deadlocks
DCompilation error
Attempts:
2 left
💡 Hint
Think about what happens when the channel has no data and the select has a default case.
Predict Output
intermediate
2:00remaining
Behavior of select without default case
What happens when this Go program runs?
Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan int)
	select {
	case val := <-ch:
		fmt.Println("Received", val)
	}
}
APrints "Received 0"
BPrints nothing and exits
CProgram deadlocks
DCompilation error
Attempts:
2 left
💡 Hint
Consider what happens when select waits on a channel with no sender and no default case.
🔧 Debug
advanced
2:00remaining
Why does this select with default case print twice?
This Go program prints "default" twice. Why?
Go
package main
import (
	"fmt"
)
func main() {
	ch := make(chan int)
	for i := 0; i < 2; i++ {
		select {
		case val := <-ch:
			fmt.Println("Received", val)
		default:
			fmt.Println("default")
		}
	}
}
ABecause the channel sends two zeros automatically
BBecause the default case runs immediately both times since channel is empty
CBecause the select statement loops twice and receives two values
DBecause the program has a race condition causing duplicate prints
Attempts:
2 left
💡 Hint
Think about what happens when the channel is empty and select has a default case inside a loop.
🧠 Conceptual
advanced
1:30remaining
Purpose of default case in select
What is the main purpose of the default case in a Go select statement?
ATo execute code immediately if no other case is ready, preventing blocking
BTo close all channels automatically when no case matches
CTo prioritize one channel over others in the select
DTo catch runtime errors during channel operations
Attempts:
2 left
💡 Hint
Think about how select behaves when no channel operation can proceed.
Predict Output
expert
2:30remaining
Output of select with multiple cases and default
What is the output of this Go program?
Go
package main
import (
	"fmt"
	"time"
)
func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)
	go func() {
		time.Sleep(50 * time.Millisecond)
		ch2 <- 42
	}()
	select {
	case val := <-ch1:
		fmt.Println("Received from ch1", val)
	case val := <-ch2:
		fmt.Println("Received from ch2", val)
	default:
		fmt.Println("No channel ready")
	}
}
AProgram deadlocks
BReceived from ch1 0
CReceived from ch2 42
DNo channel ready
Attempts:
2 left
💡 Hint
Consider the timing of the goroutine sending to ch2 and when select runs.