Learn about error handling in Go with a focus on custom error types, fmt.Errorf & errors.Join, and advanced mechanisms like panic, recover, and defer. These 30 MCQs will help sharpen your skills.
Chapter 3: Error Handling in Go – MCQs
1. Custom Error Types (10 Questions)
Which interface must a custom error type implement in Go? a) Error() method b) Message() method c) String() method d) Describe() method
What is the primary purpose of defining custom error types in Go? a) To include custom error messages b) To implement custom error logging c) To distinguish different error categories d) To handle system errors
What type is returned by the Error() method in a custom error type? a) int b) string c) error d) fmt.Stringer
How would you define a custom error type in Go? a) type MyError string b) type MyError struct { message string } c) error MyError d) type MyError() error
Which of the following methods is used to implement the error interface in a custom error type? a) Message() b) Details() c) String() d) Error()
How do you create a new instance of a custom error type in Go? a) MyError("Custom error") b) new(MyError).Message("Custom error") c) MyError{Message: "Custom error"} d) MyError("Custom error message")
Which of the following is true about custom error types in Go? a) They can include additional context, such as an error code or timestamp b) They can only contain an error message c) They are required to be primitive types d) They are only used for system errors
Can you use a custom error type as a wrapped error in Go? a) Yes, by using fmt.Errorf b) No, custom error types cannot be wrapped c) Yes, by implementing Wrap() method d) Yes, but only using the errors.Wrap() function
How can you access the underlying error message in a custom error type? a) Use error.Message() b) Use error.Error() c) Access it via a custom method d) Both b and c
What is the recommended approach for returning custom error types in Go functions? a) Always use fmt.Errorf b) Return nil if no error occurs c) Return a custom error instance with relevant context d) Never return custom errors
2. The fmt.Errorf and errors.Join Functions (10 Questions)
What does the fmt.Errorf() function return in Go? a) A custom error type b) A string with error details c) An error object d) A boolean indicating success
Which of the following is the correct syntax for using fmt.Errorf() to create an error? a) fmt.Errorf("error message") b) fmt.Errorf{message: "error message"} c) fmt.Errorf("error: %s", "message") d) fmt.Errorf("error message %w")
What does the %w verb do in fmt.Errorf()? a) Wraps an existing error b) Adds a new error message c) Formats the error d) Writes an error to the console
How does the errors.Join() function work in Go? a) Joins multiple error messages into one error b) Joins errors into a string c) Combines errors with additional context d) Both a and c
Can you use errors.Join() to combine multiple errors into one? a) Yes b) No c) Only for custom errors d) Only for fmt.Errorf errors
Which of the following best describes the fmt.Errorf function? a) It prints error messages directly to the console b) It formats error messages with additional information c) It checks if an error exists d) It handles panic scenarios
When should you use fmt.Errorf() in Go? a) To format an error message with dynamic values b) To create a new error with a predefined message c) To check the error type d) To recover from a panic
What happens when you use the errors.Join() function with multiple errors? a) It concatenates the error messages b) It returns the first error c) It returns a new error containing all the joined errors d) It ignores the errors
Is fmt.Errorf() useful for wrapping errors? a) Yes, using the %w verb b) No, it only formats the error c) Yes, but only in the context of panic d) No, errors.Join() is better
How does errors.Join() treat errors passed to it? a) It ignores errors with a nil value b) It throws a compile-time error c) It wraps errors in a new error object d) It returns an array of errors
3. Panic, Recover, and Defer (10 Questions)
What is the purpose of panic() in Go? a) To terminate the program immediately b) To print an error message c) To handle custom error types d) To raise an exception and stop execution
What happens when a panic() is called in Go? a) The program continues running normally b) The current function stops, and execution continues from the next statement c) The program stops, and deferred functions are executed d) The error is logged, but execution continues
How do you recover from a panic in Go? a) Using the panic() function b) Using the recover() function inside a deferred function c) Using defer statement only d) By raising another panic
Which of the following statements about defer is true? a) defer executes after a function returns, even if there is no panic b) defer is executed before the function exits c) defer stops the program from panicking d) defer is used only in error handling
What happens if multiple defer statements are present in a function? a) They execute in the reverse order of their appearance b) They execute in the same order as they appear c) They are ignored if there is a panic d) They are executed randomly
What is the behavior of recover() inside a defer statement? a) It catches the panic and continues normal execution b) It stops the program from running c) It logs the error and exits d) It creates a new error
Can recover() catch a panic that occurs outside the current goroutine? a) Yes b) No c) Only in the main goroutine d) Only in the deferred function
Which statement about panic recovery is incorrect? a) recover() can only be used inside a deferred function b) Once a panic occurs, the program immediately halts execution c) recover() allows you to handle the panic and resume normal execution d) panic() causes the program to stop immediately, and recover() can resume execution
Which of the following would trigger a panic in Go? a) Index out of bounds error b) Division by zero c) Calling a function with wrong arguments d) All of the above
When should panic() be used in Go? a) To handle recoverable errors b) For serious errors that cannot be handled c) To control program flow d) To throw custom error messages
Answers
QNo
Answer (Option with the text)
1
a) Error() method
2
c) To distinguish different error categories
3
b) string
4
b) type MyError struct { message string }
5
d) Error()
6
c) MyError{Message: "Custom error"}
7
a) They can include additional context, such as an error code or timestamp
8
a) Yes, by using fmt.Errorf
9
d) Both b and c
10
c) Return a custom error instance with relevant context
11
c) An error object
12
c) fmt.Errorf("error: %s", "message")
13
a) Wraps an existing error
14
d) Both a and c
15
a) Yes
16
b) It formats error messages with additional information
17
a) To format an error message with dynamic values
18
c) It returns a new error containing all the joined errors
19
a) Yes, using the %w verb
20
c) It returns an array of errors
21
d) To raise an exception and stop execution
22
c) The program stops, and deferred functions are executed
23
b) Using the recover() function inside a deferred function
24
a) defer executes after a function returns, even if there is no panic
25
a) They execute in the reverse order of their appearance
26
a) It catches the panic and continues normal execution
27
b) No
28
b) Once a panic occurs, the program immediately halts execution