Sample questions
Question 1Easy
Which go test flag prints the name and result of each test as it runs?
- -run
- -v
- -count
- -cover
Answer: B. -v
Explanation
The -v (verbose) flag prints each test name with its PASS or FAIL result, along with any t.Log output.
Step-by-step solution
- go test shows only failures by default.
- -v prints every test with its status.
- Use -run to limit which tests run.
Why the other options are wrong
- A. -run chooses which tests to execute by name pattern; it does not add test-name lines.
- C. -count repeats each test a set number of times.
- D. -cover reports code coverage, not individual test names.
Tip: Add -v while debugging a failing test.
Question 2Medium
What is the Go convention for a context.Context parameter?
- It is the first parameter, usually named ctx
- It is the last parameter, after all the other arguments
- It is stored in a package-level global variable for every call
- It is kept as a field in every struct that calls the API
Answer: A. It is the first parameter, usually named ctx
Explanation
Go convention passes context.Context as the first argument, named ctx, so cancellation and deadlines flow down the call chain.
Step-by-step solution
- Functions that may block or be cancelled take a ctx.
- Put ctx first in the parameter list.
- Pass the same ctx into the functions they call.
Why the other options are wrong
- B. Go convention puts ctx first, so callers see it at a glance.
- C. Globals hide cancellation; pass ctx through parameters instead.
- D. Storing ctx in structs is discouraged; pass it as a parameter.
Tip: ctx first, always: func Fetch(ctx context.Context, id int).
Question 3Medium
After ctx, cancel := context.WithTimeout(parent, 2*time.Second), why is defer cancel() recommended?
- It extends the timeout to be longer
- It releases the context's resources even if the work ends before the timeout
- It is required to start the timer
- It stops the whole program
Answer: B. It releases the context's resources even if the work ends before the timeout
Explanation
The context keeps a timer and resources until cancelled or expired. Calling cancel frees them promptly when the work is done early.
Step-by-step solution
WithTimeout creates a timer.- If work ends early the timer still exists.
cancel() stops it and frees resources.
Why the other options are wrong
- A. Cancel never lengthens a deadline.
- C. The timer starts at creation.
- D. It cancels the context, not the program.
Question 4Medium
What is the idiomatic Go way to test many input and output cases?
- One separate test function per case, copy-pasted from the first
- A table of cases in a slice, looped over with t.Run
- A single test that calls os.Exit on the first failure it sees
- Global variables set in init(), shared by every test in the file
Answer: B. A table of cases in a slice, looped over with t.Run
Explanation
Table-driven tests put each case in a slice of structs. A loop runs each one with t.Run, which gives each subtest its own name and result.
Step-by-step solution
- Define a slice of structs, one row per case.
- Loop over the rows.
- Call t.Run with a name for each row.
- Report failures with t.Errorf or t.Fatalf.
Why the other options are wrong
- A. It works, but repeats code; a table with t.Run is the usual Go style.
- C. os.Exit skips the test framework's reporting; use t.Error or t.Fatal.
- D. Globals tie tests together; each case belongs in its own table row.
Tip: Give each row a name, so a failing subtest shows its own name in the output.
Question 5Medium
What is the difference between t.Error and t.Fatal in a Go test?
- They do the same thing, and only the name is different
- t.Error only logs; t.Fatal stops the whole test binary
- t.Error skips the test to save time; t.Fatal only logs the message
- t.Error marks a failure and keeps running; t.Fatal stops the test
Answer: D. t.Error marks a failure and keeps running; t.Fatal stops the test
Explanation
Error records a failure and lets the rest of the test run, so you see more failures. Fatal records the failure and stops the current test function right away.
Step-by-step solution
- Error records a failure.
- The test function keeps going after Error.
- Fatal records a failure and stops that test function.
Why the other options are wrong
- A. Only Fatal stops the current test function.
- B. Fatal stops only the current test function, not the whole test run.
- C. Neither one skips the test; both mark it as failed.
Tip: Use Fatal when the next lines depend on the value that just failed, such as a nil pointer.
Question 6Hard
What is printed?
ctx, cancel := context.WithCancel(context.Background())
cancel()
cancel()
fmt.Println(ctx.Err())
- <nil>
- context deadline exceeded
- context canceled
- A panic, because cancel was called twice
Answer: C. context canceled
Explanation
Calling cancel marks the context as done, and Err then returns context.Canceled. The second call to cancel does nothing, so there is no panic.
Step-by-step solution
- cancel() closes the context's Done channel.
- Err() then returns context.Canceled.
- The second cancel() call does nothing.
- fmt.Println prints context canceled.
Why the other options are wrong
- A. After cancel, Err returns a non-nil error, not nil.
- B. That error is for timeouts and deadlines, not for a manual cancel.
- D. Calling cancel more than once is safe and does nothing extra.
Tip: Calling cancel more than once is safe, which is why defer cancel() is a common pattern.
Practise all 6 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 →