Bird
Raised Fist0
Goprogramming~20 mins

Panic behavior in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Panic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go program with panic and recover?
Consider the following Go code. What will it print when run?
Go
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")
}
A
Recovered from panic: something went wrong
Start
B
Start
End
C
Start
End
Recovered from panic: something went wrong
D
Start
Recovered from panic: something went wrong
Attempts:
2 left
💡 Hint
Remember that code after panic is not executed unless recovered.
❓ Predict Output
intermediate
2:00remaining
What happens when panic is not recovered?
What will be the output of this Go program?
Go
package main
import "fmt"
func main() {
    fmt.Println("Before panic")
    panic("fatal error")
    fmt.Println("After panic")
}
A
Before panic
fatal error
B
Before panic
After panic
C
Before panic
panic: fatal error
exit status 2
D
panic: fatal error
exit status 2
Attempts:
2 left
💡 Hint
If panic is not recovered, program crashes and prints panic message.
🔧 Debug
advanced
2:00remaining
Identify the panic cause in this Go code
What causes the panic in this Go program?
Go
package main
func main() {
    var s []int
    println(s[0])
}
AIndex out of range panic because slice is nil and empty
BNil pointer dereference panic
CCompilation error due to invalid slice access
DNo panic, prints zero
Attempts:
2 left
💡 Hint
Accessing an element of an empty slice causes panic.
❓ Predict Output
advanced
2:00remaining
What is the output of nested panics with recover?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("First recover:", r)
        }
    }()
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Second recover:", r)
            panic("new panic")
        }
    }()
    panic("initial panic")
}
A
First recover: initial panic
Second recover: new panic
B
Second recover: initial panic
First recover: new panic
CSecond recover: initial panic
DFirst recover: new panic
Attempts:
2 left
💡 Hint
Deferred functions run in reverse order. Recover can catch panics in each defer.
❓ Predict Output
expert
2:00remaining
How many items are in the map after panic in this Go code?
What is the length of the map m after running this program?
Go
package main
import "fmt"
func main() {
    m := make(map[int]int)
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered panic")
        }
    }()
    for i := 0; i < 3; i++ {
        if i == 2 {
            panic("stop")
        }
        m[i] = i * 10
    }
    fmt.Println(len(m))
}
A2
B3
C0
DProgram does not print length due to panic
Attempts:
2 left
💡 Hint
Panic occurs before adding the third item, but map keeps previous entries.

Practice

(1/5)
1. What happens when a panic is called in a Go program?
easy
A. The program ignores the panic and continues running.
B. The program restarts automatically.
C. The program stops immediately and prints an error message.
D. The program logs the error but continues execution.

Solution

  1. Step 1: Understand panic behavior

    In Go, calling panic immediately stops normal execution and begins panicking.
  2. Step 2: Effect on program flow

    The program prints a stack trace and error message, then terminates unless recovered.
  3. Final Answer:

    The program stops immediately and prints an error message. -> Option C
  4. Quick Check:

    Panic stops program = B [OK]
Hint: Panic always stops the program immediately [OK]
Common Mistakes:
  • Thinking panic allows program to continue
  • Confusing panic with error return
  • Assuming panic restarts the program
2. Which of the following is the correct syntax to cause a panic with the message "error occurred"?
easy
A. panic("error occurred";)
B. panic("error occurred")
C. panic: "error occurred"
D. panic = "error occurred"

Solution

  1. Step 1: Recall panic syntax

    Panic is a built-in function called with parentheses and a string argument.
  2. Step 2: Check each option

    panic("error occurred") uses correct function call syntax. Others have invalid assignment, colon, or extra semicolon.
  3. Final Answer:

    panic("error occurred") -> Option B
  4. Quick Check:

    Panic is a function call = D [OK]
Hint: Panic is called like a function with parentheses [OK]
Common Mistakes:
  • Using assignment instead of function call
  • Adding extra semicolons inside panic call
  • Using colon instead of parentheses
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    fmt.Println("Start")
    panic("something went wrong")
    fmt.Println("End")
}
medium
A. Start End
B. something went wrong panic stack trace
C. Start
D. Start something went wrong panic stack trace

Solution

  1. Step 1: Trace program execution

    The program prints "Start" first, then calls panic with a message.
  2. Step 2: Effect of panic on following code

    After panic, the program stops immediately, so "End" is not printed. Panic message and stack trace appear.
  3. Final Answer:

    Start something went wrong panic stack trace -> Option D
  4. Quick Check:

    Panic stops after printing Start = C [OK]
Hint: Panic prints message and stops; code after panic not run [OK]
Common Mistakes:
  • Expecting code after panic to run
  • Ignoring panic message output
  • Confusing panic output with normal print
4. Identify the error in this Go code snippet:
func divide(a, b int) int {
    if b == 0 {
        panic("division by zero")
    }
    return a / b
}

func main() {
    result := divide(10, 0)
    fmt.Println(result)
}
medium
A. Missing import of "fmt" package
B. Cannot panic inside a function
C. Division by zero is allowed, no panic needed
D. Function divide should return error instead of int

Solution

  1. Step 1: Check code for syntax errors

    The code uses fmt.Println but does not import "fmt" package.
  2. Step 2: Validate panic usage

    Panic is allowed inside functions; division by zero should panic to avoid runtime error.
  3. Final Answer:

    Missing import of "fmt" package -> Option A
  4. Quick Check:

    Missing import causes compile error = A [OK]
Hint: Check imports when using fmt functions [OK]
Common Mistakes:
  • Thinking panic is not allowed in functions
  • Ignoring missing imports
  • Assuming division by zero returns zero
5. You want to write a Go function that safely divides two integers and returns the result. If the divisor is zero, it should panic with "division by zero". Which of these implementations correctly uses panic and recover to handle the error and return zero instead of crashing the program?
func safeDivide(a, b int) int {
    // Your code here
}

func main() {
    fmt.Println(safeDivide(10, 0))
}
hard
A. func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic") } }() if b == 0 { panic("division by zero") } return a / b }
B. func safeDivide(a, b int) int { if b == 0 { panic("division by zero") } return a / b }
C. func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { return 0 } }() if b == 0 { panic("division by zero") } return a / b }
D. func safeDivide(a, b int) int { if b == 0 { return 0 } return a / b }

Solution

  1. Step 1: Understand panic and recover usage

    Recover must be called inside a deferred function to catch panic and prevent program crash.
  2. Step 2: Analyze each option

    func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic") } }() if b == 0 { panic("division by zero") } return a / b } uses defer with recover to catch panic and prints a message, then continues. It returns the division result or panics if divisor is zero. func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { return 0 } }() if b == 0 { panic("division by zero") } return a / b } tries to return inside defer, which does not affect the outer function's return. func safeDivide(a, b int) int { if b == 0 { panic("division by zero") } return a / b } panics without recovery. func safeDivide(a, b int) int { if b == 0 { return 0 } return a / b } returns 0 without panic.
  3. Final Answer:

    Option A correctly uses panic and recover to handle error and avoid crash. -> Option A
  4. Quick Check:

    Defer recover catches panic = A [OK]
Hint: Use defer with recover to catch panic safely [OK]
Common Mistakes:
  • Returning inside defer does not change function return
  • Not using recover causes program crash
  • Ignoring panic and returning zero silently
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