0
0
Goprogramming~20 mins

Panic behavior in Go - Practice Problems & Coding Challenges

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