Bird
Raised Fist0
Goprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the default case do in a Go select statement?

easy
A. Closes all channels automatically
B. Blocks until a channel is ready
C. Runs immediately if no other channel operations are ready
D. Sends data to all channels simultaneously

Solution

  1. Step 1: Understand the role of select

    The select statement waits on multiple channel operations.
  2. Step 2: Identify the purpose of default

    The default case runs immediately if no channels are ready, preventing blocking.
  3. Final Answer:

    Runs immediately if no other channel operations are ready -> Option C
  4. Quick Check:

    default runs when no channels ready = A [OK]
Hint: Default runs instantly if no channels are ready [OK]
Common Mistakes:
  • Thinking default blocks until a channel is ready
  • Assuming default closes channels
  • Believing default sends data to all channels
2.

Which of the following is the correct syntax for using a default case in a Go select?

select {
  case msg := <-ch1:
    fmt.Println(msg)
  // What goes here?
}
easy
A. default: fmt.Println("No message received")
B. else: fmt.Println("No message received")
C. fallback: fmt.Println("No message received")
D. otherwise: fmt.Println("No message received")

Solution

  1. Step 1: Recall Go select syntax

    The default keyword is used for the fallback case in select.
  2. Step 2: Match correct keyword

    Only default is valid; else, fallback, and otherwise are invalid in Go select.
  3. Final Answer:

    default: fmt.Println("No message received") -> Option A
  4. Quick Check:

    Use default keyword in select = C [OK]
Hint: Default is the only valid fallback keyword in select [OK]
Common Mistakes:
  • Using else instead of default
  • Trying fallback or otherwise keywords
  • Omitting the colon after default
3.

What will be the output of this Go program?

package main
import (
  "fmt"
  "time"
)
func main() {
  ch := make(chan string)
  select {
  case msg := <-ch:
    fmt.Println(msg)
  default:
    fmt.Println("No message received")
  }
}
medium
A. Compilation error
B. Program blocks forever
C. Empty string printed
D. No message received

Solution

  1. Step 1: Analyze channel state

    The channel ch is created but no data is sent, so it's empty and no receive is ready.
  2. Step 2: Understand select with default

    Since no data is ready on ch, the default case runs immediately, printing "No message received".
  3. Final Answer:

    No message received -> Option D
  4. Quick Check:

    No data ready triggers default = A [OK]
Hint: Default runs if channel has no data ready [OK]
Common Mistakes:
  • Expecting program to block waiting for channel
  • Thinking empty string is printed
  • Assuming compilation error due to empty channel
4.

Identify the error in this Go code snippet using select with a default case:

select {
case msg := <-ch:
  fmt.Println(msg)
default
  fmt.Println("No message")
}
medium
A. Default case must be last
B. Missing colon after default keyword
C. Cannot use variable msg in select
D. Channel ch is not initialized

Solution

  1. Step 1: Check default syntax

    The default case requires a colon (:) after the keyword.
  2. Step 2: Identify missing colon

    The code has default without a colon, causing a syntax error.
  3. Final Answer:

    Missing colon after default keyword -> Option B
  4. Quick Check:

    Default needs colon (:) = D [OK]
Hint: Always put colon after default in select [OK]
Common Mistakes:
  • Forgetting colon after default
  • Assuming default must be last case
  • Confusing variable scope in select
5.

Consider this Go program snippet:

ch1 := make(chan int)
ch2 := make(chan int)
go func() {
  time.Sleep(100 * time.Millisecond)
  ch1 <- 1
}()
go func() {
  time.Sleep(200 * time.Millisecond)
  ch2 <- 2
}()
select {
case val := <-ch1:
  fmt.Println("Received from ch1:", val)
case val := <-ch2:
  fmt.Println("Received from ch2:", val)
default:
  fmt.Println("No channels ready")
}

What is the most likely output when this select runs?

hard
A. No channels ready
B. Received from ch1: 1
C. Received from ch2: 2
D. Program deadlocks

Solution

  1. Step 1: Understand goroutine timing

    The goroutines send to channels after 100ms and 200ms delays, but the select runs immediately.
  2. Step 2: Check channel readiness at select

    At the moment select runs, neither channel has data ready, so no case is ready.
  3. Step 3: Default case triggers

    Because no channels are ready, the default case runs, printing "No channels ready".
  4. Final Answer:

    No channels ready -> Option A
  5. Quick Check:

    Default runs if no channel ready = B [OK]
Hint: Select runs immediately; default if no channel ready [OK]
Common Mistakes:
  • Assuming select waits for channels
  • Expecting output from ch1 or ch2 immediately
  • Thinking program deadlocks without default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard