MCQs on Error Handling | Go

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)

  1. Which interface must a custom error type implement in Go?
    a) Error() method
    b) Message() method
    c) String() method
    d) Describe() method
  2. 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
  3. What type is returned by the Error() method in a custom error type?
    a) int
    b) string
    c) error
    d) fmt.Stringer
  4. 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
  5. 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()
  6. 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")
  7. 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
  8. 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
  9. 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
  10. 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)

  1. 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
  2. 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")
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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)

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

QNoAnswer (Option with the text)
1a) Error() method
2c) To distinguish different error categories
3b) string
4b) type MyError struct { message string }
5d) Error()
6c) MyError{Message: "Custom error"}
7a) They can include additional context, such as an error code or timestamp
8a) Yes, by using fmt.Errorf
9d) Both b and c
10c) Return a custom error instance with relevant context
11c) An error object
12c) fmt.Errorf("error: %s", "message")
13a) Wraps an existing error
14d) Both a and c
15a) Yes
16b) It formats error messages with additional information
17a) To format an error message with dynamic values
18c) It returns a new error containing all the joined errors
19a) Yes, using the %w verb
20c) It returns an array of errors
21d) To raise an exception and stop execution
22c) The program stops, and deferred functions are executed
23b) Using the recover() function inside a deferred function
24a) defer executes after a function returns, even if there is no panic
25a) They execute in the reverse order of their appearance
26a) It catches the panic and continues normal execution
27b) No
28b) Once a panic occurs, the program immediately halts execution
29d) All of the above
30b) For serious errors that cannot be handled

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top