MCQs on Advanced Error Handling | Go

In this chapter, we dive deep into advanced error handling techniques in Go, including error wrapping with context, using third-party packages like pkg/errors, and best practices in logging strategies and tools.


Chapter 3: Advanced Error Handling in Go – MCQs

1. Wrapping Errors with Context (10 Questions)

  1. What is the purpose of wrapping errors in Go?
    a) To add more context to the error
    b) To change the type of the error
    c) To propagate the error to another function
    d) To log the error automatically
  2. How can you wrap an existing error in Go?
    a) Using fmt.Errorf with %w verb
    b) Using errors.New()
    c) Using error.Wrap()
    d) Using fmt.Print()
  3. Which of the following is true about error wrapping in Go?
    a) Wrapping an error adds extra details without changing the original error
    b) Wrapping an error changes the original error
    c) Wrapping errors is not allowed in Go
    d) Wrapped errors are ignored by defer statements
  4. How do you check the cause of a wrapped error in Go?
    a) Using errors.Cause()
    b) Using fmt.Print()
    c) Using error.Unwrap()
    d) Both a and c
  5. What does the Unwrap() function do in Go’s error handling?
    a) Extracts the original error from a wrapped error
    b) Logs the error automatically
    c) Changes the error type
    d) Catches panic in a function
  6. In which Go version was error wrapping with %w introduced?
    a) Go 1.8
    b) Go 1.10
    c) Go 1.13
    d) Go 1.15
  7. What happens if an error is wrapped multiple times?
    a) The original error is lost
    b) Only the most recent wrap is available
    c) The original error can still be accessed using errors.Unwrap()
    d) All wrapped errors are concatenated into a single message
  8. How can you use fmt.Errorf to wrap an error with context?
    a) fmt.Errorf("context: %w", err)
    b) fmt.Errorf("context: %s", err)
    c) fmt.Errorf("context %w: %s", err)
    d) fmt.Errorf("context: %v", err)
  9. Why is it important to provide context when wrapping errors?
    a) It helps to log the error
    b) It allows tracing the error’s origin
    c) It prevents panic
    d) It reduces the error message size
  10. Which function is used to wrap an error with additional context and cause in Go?
    a) errors.Wrap()
    b) errors.New()
    c) fmt.Errorf()
    d) error.Wrap()

2. Third-party Error Packages (10 Questions)

  1. What is the primary use of third-party error packages in Go?
    a) To standardize error messages
    b) To log errors automatically
    c) To add additional functionality to error handling
    d) To replace the standard error type
  2. Which package provides the Wrap function to wrap errors with context?
    a) errors
    b) fmt
    c) pkg/errors
    d) log
  3. What does the pkg/errors package offer for error handling?
    a) Error wrapping
    b) Stack traces
    c) Error formatting
    d) All of the above
  4. Which of the following is a benefit of using pkg/errors over the standard Go error handling?
    a) Automatic logging of errors
    b) Support for error wrapping and stack traces
    c) Error message shortening
    d) Better error propagation
  5. How do you add a stack trace to an error using pkg/errors?
    a) errors.Wrap(err, "message")
    b) errors.AddStackTrace(err)
    c) errors.NewStackTrace(err)
    d) errors.WithStack(err)
  6. What is the purpose of the errors.Wrapf() function in the pkg/errors package?
    a) To format an error message without changing the original error
    b) To create a new error with no context
    c) To log the error automatically
    d) To create an error with a custom error code
  7. Which of the following does the pkg/errors package help you avoid?
    a) Writing verbose error messages
    b) Adding context and stack traces to every error
    c) Handling all errors in one place
    d) Ignoring errors completely
  8. How do you check if an error is of a specific type in the pkg/errors package?
    a) Using errors.Is()
    b) Using errors.As()
    c) Using errors.Unwrap()
    d) Using errors.Compare()
  9. Which Go tool is commonly used alongside third-party error packages for error handling?
    a) go test
    b) log
    c) fmt
    d) pkg/errors
  10. What is one disadvantage of using third-party error packages like pkg/errors?
    a) It increases code verbosity
    b) It does not support stack traces
    c) It is not compatible with all Go versions
    d) It reduces error handling capabilities

3. Logging Strategies and Tools (10 Questions)

  1. What is the primary goal of logging in Go?
    a) To log successful function calls
    b) To track program flow
    c) To capture and store error details for debugging
    d) To replace error handling
  2. Which Go package is commonly used for logging in Go applications?
    a) log
    b) fmt
    c) errors
    d) logger
  3. What is the difference between log.Fatal() and log.Panic() in Go?
    a) log.Fatal() writes the error to the log and terminates the program, log.Panic() triggers a panic
    b) log.Panic() writes the error and terminates the program
    c) log.Fatal() writes the error without terminating the program
    d) There is no difference
  4. How can you configure logging for different log levels (e.g., info, error) in Go?
    a) By using the log package
    b) By using third-party logging packages like logrus
    c) By setting custom log levels manually
    d) By overriding the fmt package
  5. Which of the following is an example of structured logging in Go?
    a) log.Println("Error occurred")
    b) log.Printf("Error at %s: %s", time.Now(), err)
    c) log.Fatal("Critical error!")
    d) log.Debug("Debugging...")
  6. What is a key benefit of structured logging?
    a) Easier to read and search logs
    b) Logs are stored in plain text
    c) Reduced error handling complexity
    d) Logs are ignored during program execution
  7. Which logging tool in Go supports JSON logging out of the box?
    a) log
    b) logrus
    c) fmt
    d) zap
  8. How can you write custom log formats in Go?
    a) By using the log package’s SetFlags() function
    b) By using third-party packages like logrus or zap
    c) By writing to a log file manually
    d) By using the fmt package
  9. What is the advantage of using logrus over the built-in log package?
    a) It supports log rotation
    b) It supports structured logging and multiple log levels
    c) It is more lightweight
    d) It automatically handles error messages
  10. What is the purpose of using defer in logging?
    a) To delay logging messages until after the function completes
    b) To stop logging messages
    c) To change the log level dynamically
    d) To handle panic situations in logging

Answers

QNoAnswer (Option with the text)
1a) To add more context to the error
2a) Using fmt.Errorf with %w verb
3a) Wrapping an error adds extra details without changing the original error
4d) Both a and c
5a) Extracts the original error from a wrapped error
6c) Go 1.13
7c) The original error can still be accessed using errors.Unwrap()
8a) fmt.Errorf("context: %w", err)
9b) It allows tracing the error’s origin
10c) fmt.Errorf()
11c) To add additional functionality to error handling
12c) pkg/errors
13d) All of the above
14b) Support for error wrapping and stack traces
15d) errors.WithStack(err)
16a) errors.Wrap(err, "message")
17b) Adding context and stack traces to every error
18d) Using errors.Compare()
19a) go test
20c) It is not compatible with all Go versions
21c) To capture and store error details for debugging
22a) log
23a) log.Fatal() writes the error to the log and terminates the program, log.Panic() triggers a panic
24b) By using third-party logging packages like logrus
25b) log.Printf("Error at %s: %s", time.Now(), err)
26a) Easier to read and search logs
27b) logrus
28b) By using third-party packages like logrus or zap
29b) It supports structured logging and multiple log levels
30a) To delay logging messages until after the function completes

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