Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to recover from a panic and print a message.
Go
func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
fmt.Println([1])
}
}()
return a / b
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to check if recover() is nil before printing.
Not using defer to call the recovery function.
✗ Incorrect
The recover function returns the panic value. Printing a message like "Recovered from panic!" informs the user that the panic was handled.
2fill in blank
mediumComplete the code to handle a panic and return zero instead of crashing.
Go
func safeAccess(arr []int, index int) int {
defer func() {
if r := recover(); r != nil {
[1]
}
}()
return arr[index]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to return a value inside defer without named return variables.
Not recovering from panic at all.
✗ Incorrect
Returning 0 inside the deferred function does not work because deferred functions cannot change the return value directly. The correct way is to use named return values or handle differently, but here the task expects 'return 0' as the answer to prevent crash.
3fill in blank
hardFix the error in the code to properly recover from panic and set the return value.
Go
func safeDivide(a, b int) (result int) {
defer func() {
if r := recover(); r != nil {
[1]
}
}()
result = a / b
return
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'return' inside defer which is not allowed.
Not using named return variables to modify the return value.
✗ Incorrect
Using a named return value 'result' allows the deferred function to set it to 0 when a panic occurs, preventing the program from crashing.
4fill in blank
hardFill both blanks to create a map of string to int with only keys having length greater than 3.
Go
words := []string{"go", "code", "chat", "ai"}
lengths := map[string]int{
[1]: len([2]) for _, word := range words if len(word) > 3
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the slice name 'words' instead of the variable 'word'.
Not quoting the map key properly.
✗ Incorrect
The map keys are strings, so the key should be the variable 'word' as a string key. The value is the length of the word variable.
5fill in blank
hardFill all three blanks to create a map of uppercase keys to their lengths for words longer than 2 characters.
Go
words := []string{"go", "code", "chat", "ai"}
lengths := map[string]int{
[1]: [2] for _, [3] := range words if len(word) > 2
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of the loop variable 'w'.
Not converting keys to uppercase.
✗ Incorrect
The key is the uppercase version of the word, so use strings.ToUpper(word). The value is the length of the word. The loop variable is 'w', so inside the comprehension use 'w'.