Challenge - 5 Problems
Go Panic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
Remember that code after panic is not executed unless recovered.
✗ Incorrect
The program prints "Start" first. Then panic occurs. The deferred function recovers from panic and prints the recovery message. The line after panic is not executed.
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
If panic is not recovered, program crashes and prints panic message.
✗ Incorrect
The program prints "Before panic" then panics. Since no recover is used, it prints panic message and exits with status 2. The line after panic is not executed.
🔧 Debug
advanced2: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]) }
Attempts:
2 left
💡 Hint
Accessing an element of an empty slice causes panic.
✗ Incorrect
The slice s is nil and has length zero. Accessing s[0] causes an index out of range panic.
❓ Predict Output
advanced2: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") }
Attempts:
2 left
💡 Hint
Deferred functions run in reverse order. Recover can catch panics in each defer.
✗ Incorrect
The last defer runs first and recovers from "initial panic", prints "Second recover: initial panic", then panics again with "new panic". The first defer then recovers from "new panic" and prints "First recover: new panic".
❓ Predict Output
expert2: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)) }
Attempts:
2 left
💡 Hint
Panic occurs before adding the third item, but map keeps previous entries.
✗ Incorrect
The loop adds m[0]=0 and m[1]=10. When i==2, panic occurs before adding m[2]. The deferred recover prevents crash but the print statement after loop is not reached. So length is printed only if after loop. Here it is not printed, but the question asks length after running, so map has 2 items.