Sample questions
Question 1Easy
What does the blank identifier in v, _ := f() do with the error?
- Converts it to a zero value of v
- Discards it, which hides failures if f can fail
- Logs it automatically to standard error, so the failure is still visible when debugging
- Panics if the error is non-nil
Answer: B. Discards it, which hides failures if f can fail
Explanation
Ignoring errors is legal but risky; linters flag unchecked errors.
Step-by-step solution
- The blank identifier _ ignores the second return value of f().
- Ignoring the error means any failure in f() is not checked.
- This hides failures, making it risky but legal in Go.
- So the error is discarded, which hides failures if f can fail.
- Final answer: Discards it, which hides failures if f can fail.
Why the other options are wrong
- A. The blank identifier does not convert the error; it just ignores the second value.
- C. Nothing is logged; the blank identifier writes nothing to stderr.
- D. No panic happens; the error is simply dropped, and the program keeps running.
Tip: Use _ to ignore errors but be cautious as it hides potential issues.
Question 2Easy
How does Go typically report errors?
- As an extra error return value checked by the caller
- By setting a global errno variable that the caller must check after every function call
- With try/catch exceptions
- By printing and exiting
Answer: A. As an extra error return value checked by the caller
Explanation
val, err := f();
if err != nil { ... }
is the standard pattern.
Step-by-step solution
- Go has no exceptions.
- Functions return an error as the last value.
- Callers check if err != nil.
Why the other options are wrong
- B. Go does not use errno.
- C. Go has no try/catch; panic is for unrecoverable cases.
- D. Printing and exiting is not the standard error mechanism.
Question 3Easy
- An error that stops the program immediately, like a panic, but can be caught by recover
- An error with a stack trace
- A predeclared error value, like io.EOF, that callers compare against
- A logging function
Answer: C. A predeclared error value, like io.EOF, that callers compare against
Explanation
Callers check errors.Is(err, io.EOF) to detect a specific condition.
Step-by-step solution
- A sentinel error is a fixed package-level error value.
- Example:
io.EOF. - Callers compare it with
== or errors.Is.
Why the other options are wrong
- A. It does not stop the program.
- B. Stack traces are not part of the definition.
- D. It is not a logging function.
Question 4Easy
What is the usual Go convention for a function that can fail?
- Return the error as the last return value
- Throw an exception with throw
- Return a negative number
- Set a global error flag
Answer: A. Return the error as the last return value
Explanation
Go has no exceptions for ordinary errors. A function returns (result, error) and callers check if err != nil.
Step-by-step solution
- Declare
func f() (T, error). - Return nil error on success.
- Caller tests
err != nil.
Why the other options are wrong
- B. Go has no throw statement.
- C. Negative numbers do not carry details.
- D. A global flag is not idiomatic.
Question 5Medium
What is the output?
package main
import (
"errors"
"fmt"
)
var ErrNotFound = errors.New("not found")
func main() {
err := fmt.Errorf("load user: %w", ErrNotFound)
fmt.Println(errors.Is(err, ErrNotFound), err)
}
- false load user: not found
- true not found
- Compile error
- true load user: not found
Answer: D. true load user: not found
Explanation
%w wraps the sentinel; errors.Is walks the chain and finds it.
Step-by-step solution
- errors.New creates ErrNotFound, and fmt.Errorf with %w wraps it inside err.
- The text of err is 'load user: not found'.
- errors.Is walks the wrapped chain, finds ErrNotFound, and returns true.
- Println prints 'true load user: not found', so the answer is option D.
Why the other options are wrong
- A. errors.Is finds the wrapped error, so the result is true, not false.
- B. The printed text includes the 'load user: ' prefix added by Errorf.
- C. The code compiles; both imports are used.
Tip: %w keeps the original error inside the new one, so errors.Is can still match it.
Question 6Medium
Why is comparing error messages with strings (err.Error() == "not found") discouraged?
- Errors have no Error method
- It always returns true
- String comparison between error messages is not allowed in Go and fails to compile
- Messages can change and wrapping adds text, so the comparison breaks
Answer: D. Messages can change and wrapping adds text, so the comparison breaks
Explanation
Compare sentinel values or types with errors.Is and errors.As instead.
Step-by-step solution
- Error messages may change in future versions of code.
- Wrapping errors adds extra text to the message.
- This makes string comparisons unreliable and break easily.
- Comparing with
errors.Is or errors.As handles these cases better. - So comparing strings like
err.Error() == "not found" is discouraged.
Why the other options are wrong
- A. Errors do have an Error method; every error value provides it through the error interface.
- B. The comparison does not always return true; it is true only when the text matches exactly.
- C. The code compiles fine; the problem is fragility, not a compile error.
Tip: Use errors.Is or errors.As instead of string comparisons for errors.
Question 7Medium
- Compare two error strings for equality, ignoring any context added by wrapping
- Find an error of a specific type in a wrapped chain and extract it
- Create a new error
- Convert an error to a string
Answer: B. Find an error of a specific type in a wrapped chain and extract it
Explanation
var pe *fs.PathError;
if errors.As(err, &pe) { ... }
gives access to the typed error's fields.
Step-by-step solution
- errors.As checks if an error matches a specific type in a wrapped chain.
- It allows extracting the specific error's fields from the chain.
- This is useful for handling errors of particular types accurately.
- The function returns true if the error matches the target type.
- This lets you access detailed information about the error directly.
Why the other options are wrong
- A. Comparing error strings is not what errors.As does; it matches a type in the wrapped chain.
- C. Creating an error is done with errors.New or fmt.Errorf, not errors.As.
- D. Turning an error into text is done with err.Error, not errors.As.
Tip: Use errors.As to extract typed errors from chains for better error handling.
Question 8Medium
What is printed?
err := fmt.Errorf("read: %w", io.EOF)
fmt.Println(errors.Is(err, io.EOF))
- false
- read: EOF
- true
- Compile error
Answer: C. true
Explanation
%w wraps the error, and errors.Is follows the wrap chain, so it finds io.EOF.
Step-by-step solution
Errorf with %w keeps the original error.errors.Is unwraps step by step.- It matches io.EOF.
Why the other options are wrong
- A. Wrapping preserves identity for Is.
- B. Is returns a bool.
- D. The code is valid.
Tip: Use %w, not %v, to wrap.
Question 9Medium
Good practice when returning an error up the stack is to:
- Print it and also return it at every level, so the full path appears in the logs
- Discard it and return
nil - Add context, e.g.
fmt.Errorf("open config: %w", err) - Panic immediately
Answer: C. Add context, e.g. fmt.Errorf("open config: %w", err)
Explanation
Context makes the final message useful; logging at every level causes duplicate noise.
Step-by-step solution
- When an error goes up the stack, it should gain context about what failed.
- fmt.Errorf with %w adds that context and keeps the original error for errors.Is.
- So adding context with %w is good practice, which is option C.
Why the other options are wrong
- A. Logging the same error at every level repeats messages; handle an error once.
- B. Discarding the error hides the failure.
- D. Panicking on ordinary errors crashes the program; return them instead.
Tip: Wrap errors with context as they go up, and handle or log them once.
Question 10Hard
What is the output?
package main
import (
"errors"
"fmt"
)
func main() {
e1 := errors.New("x")
e2 := errors.New("x")
fmt.Println(e1 == e2, e1.Error() == e2.Error())
}
- false true
- true false
- true true
- false false
Answer: A. false true
Explanation
errors.New returns distinct pointer values, so equal text does not make them equal errors.
Step-by-step solution
errors.New returns a pointer to a new error value each time.e1 == e2 compares pointers, which differ: false.Error() strings are both 'x': true.
Why the other options are wrong
- B. Pointers are different.
- C. Same text does not mean same value.
- D. The strings are equal.
Practise all 11 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →