Bird
Raised Fist0
Goprogramming~20 mins

Program stability concepts in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Program Stability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of deferred function with panic
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    defer fmt.Println("deferred")
    panic("panic occurred")
    fmt.Println("after panic")
}
Adeferred
B
deferred
panic: panic occurred
C
after panic
deferred
D
panic: panic occurred
deferred
Attempts:
2 left
💡 Hint
Remember that deferred functions run before the program exits due to panic.
❓ Predict Output
intermediate
2:00remaining
Recover usage in goroutine
What will this Go program print?
Go
package main
import (
    "fmt"
    "time"
)
func safeGo() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic")
        }
    }()
    panic("panic in goroutine")
}
func main() {
    go safeGo()
    time.Sleep(100 * time.Millisecond)
    fmt.Println("main finished")
}
A
main finished
Recovered from panic
B
Recovered from panic
main finished
C
panic: panic in goroutine
main finished
Dpanic: panic in goroutine
Attempts:
2 left
💡 Hint
Recover only works if called inside deferred function in the same goroutine.
❓ Predict Output
advanced
2:00remaining
Race condition detection output
What will happen when running this Go program with the race detector enabled?
Go
package main
import (
    "fmt"
    "sync"
)
func main() {
    var x int
    var wg sync.WaitGroup
    wg.Add(2)
    go func() {
        x = 1
        wg.Done()
    }()
    go func() {
        x = 2
        wg.Done()
    }()
    wg.Wait()
    fmt.Println(x)
}
APrints 1 or 2, race detector reports data race
BAlways prints 1, no race detected
CCompilation error due to missing synchronization
DAlways prints 2, no race detected
Attempts:
2 left
💡 Hint
Two goroutines write to the same variable without synchronization.
❓ Predict Output
advanced
2:00remaining
Effect of closing a channel twice
What happens when this Go program runs?
Go
package main
func main() {
    ch := make(chan int)
    close(ch)
    close(ch)
}
ACompilation error
BDeadlock error
CPanic: close of closed channel
DProgram exits normally
Attempts:
2 left
💡 Hint
Closing a channel twice is not allowed in Go.
❓ Predict Output
expert
2:00remaining
Output of select with nil channel
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var ch chan int // nil channel
    select {
    case ch <- 1:
        fmt.Println("sent 1")
    case <-ch:
        fmt.Println("received")
    default:
        fmt.Println("default case")
    }
}
Adefault case
Bsent 1
Creceived
DDeadlock
Attempts:
2 left
💡 Hint
Operations on nil channels block forever unless default case is present.

Practice

(1/5)
1. What is the main purpose of using recover() in Go programs?
easy
A. To declare a new variable
B. To start a new goroutine
C. To catch a panic and prevent the program from crashing
D. To handle normal errors returned by functions

Solution

  1. Step 1: Understand panic and recover

    Panic causes a program to stop running immediately unless recovered.
  2. Step 2: Role of recover()

    Recover is used inside a deferred function to catch the panic and allow the program to continue.
  3. Final Answer:

    To catch a panic and prevent the program from crashing -> Option C
  4. Quick Check:

    recover() catches panic = C [OK]
Hint: recover() only works inside deferred functions [OK]
Common Mistakes:
  • Thinking recover() handles normal errors
  • Using recover() outside defer
  • Confusing recover() with panic()
2. Which of the following is the correct syntax to defer a function call in Go?
easy
A. defer fmt.Println("Hello")
B. defer = fmt.Println("Hello")
C. defer: fmt.Println("Hello")
D. defer -> fmt.Println("Hello")

Solution

  1. Step 1: Recall defer syntax

    In Go, defer is a keyword placed before a function call to delay its execution.
  2. Step 2: Check each option

    Only defer fmt.Println("Hello") uses 'defer' correctly before the function call without extra symbols.
  3. Final Answer:

    defer fmt.Println("Hello") -> Option A
  4. Quick Check:

    defer + function call = D [OK]
Hint: defer must be followed directly by a function call [OK]
Common Mistakes:
  • Adding '=' after defer
  • Using ':' or '->' after defer
  • Not calling a function after defer
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic:", r)
        }
    }()
    fmt.Println("Start")
    panic("Something went wrong")
    fmt.Println("End")
}
medium
A. Start panic: Something went wrong
B. Start End Recovered from panic: Something went wrong
C. Start End
D. Start Recovered from panic: Something went wrong

Solution

  1. Step 1: Trace program execution

    "Start" is printed first. Then panic is called, which triggers the deferred function.
  2. Step 2: Deferred function recovers panic

    The deferred anonymous function calls recover(), catches the panic, and prints the recovery message. Execution does not continue after panic call, so "End" is not printed.
  3. Final Answer:

    Start Recovered from panic: Something went wrong -> Option D
  4. Quick Check:

    panic caught by recover = A [OK]
Hint: Deferred recover runs on panic, then execution continues after panic [OK]
Common Mistakes:
  • Thinking code after panic does not run
  • Missing recover() inside defer
  • Confusing panic message with normal print
4. Identify the error in this Go code that tries to recover from a panic:
package main
import "fmt"
func main() {
    if r := recover(); r != nil {
        fmt.Println("Recovered:", r)
    }
    panic("Oops")
}
medium
A. recover() must be called inside a deferred function
B. panic() must be called before recover()
C. recover() cannot be used with panic()
D. fmt.Println() cannot be used inside if statement

Solution

  1. Step 1: Understand recover usage

    recover() only works if called inside a deferred function; here it is called directly in main.
  2. Step 2: Analyze code flow

    Since recover() is not deferred, it cannot catch the panic, so the program will crash.
  3. Final Answer:

    recover() must be called inside a deferred function -> Option A
  4. Quick Check:

    recover inside defer = B [OK]
Hint: Always call recover() inside defer to catch panics [OK]
Common Mistakes:
  • Calling recover() directly without defer
  • Thinking recover() catches errors, not panics
  • Placing panic before recover without defer
5. You want to write a stable Go program that logs a message when a panic occurs but continues running. Which code snippet correctly implements this behavior?
hard
A. func main() { if r := recover(); r != nil { fmt.Println("Panic caught:", r) } panic("Crash") fmt.Println("Program continues") }
B. func main() { defer func() { if r := recover(); r != nil { fmt.Println("Panic caught:", r) } }() panic("Crash") fmt.Println("Program continues") }
C. func main() { panic("Crash") defer func() { if r := recover(); r != nil { fmt.Println("Panic caught:", r) } }() fmt.Println("Program continues") }
D. func main() { defer fmt.Println("Program continues") panic("Crash") }

Solution

  1. Step 1: Check defer and recover placement

    func main() { defer func() { if r := recover(); r != nil { fmt.Println("Panic caught:", r) } }() panic("Crash") fmt.Println("Program continues") } correctly defers a function that calls recover() to catch panic and print a message.
  2. Step 2: Verify program flow

    After panic, deferred recover runs, preventing crash and allowing program to continue, executing code after panic.
  3. Final Answer:

    Uses defer func() with recover() before panic("Crash") -> Option B
  4. Quick Check:

    defer + recover to catch panic = A [OK]
Hint: Defer recover before panic to handle it safely [OK]
Common Mistakes:
  • Calling recover() outside defer
  • Placing defer after panic
  • Not handling panic at all
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