Testing & Context Questions with Answers

6 practice questions on Testing & Context (Go) with answers and step-by-step solutions. Try 6 samples below, then practise all 6 free in the AptiNest app.

Sample questions

Question 1Easy

Which go test flag prints the name and result of each test as it runs?

  1. -run
  2. -v
  3. -count
  4. -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

  1. go test shows only failures by default.
  2. -v prints every test with its status.
  3. 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?

  1. It is the first parameter, usually named ctx
  2. It is the last parameter, after all the other arguments
  3. It is stored in a package-level global variable for every call
  4. 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

  1. Functions that may block or be cancelled take a ctx.
  2. Put ctx first in the parameter list.
  3. 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?

  1. It extends the timeout to be longer
  2. It releases the context's resources even if the work ends before the timeout
  3. It is required to start the timer
  4. 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

  1. WithTimeout creates a timer.
  2. If work ends early the timer still exists.
  3. 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?

  1. One separate test function per case, copy-pasted from the first
  2. A table of cases in a slice, looped over with t.Run
  3. A single test that calls os.Exit on the first failure it sees
  4. 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

  1. Define a slice of structs, one row per case.
  2. Loop over the rows.
  3. Call t.Run with a name for each row.
  4. 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?

  1. They do the same thing, and only the name is different
  2. t.Error only logs; t.Fatal stops the whole test binary
  3. t.Error skips the test to save time; t.Fatal only logs the message
  4. 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

  1. Error records a failure.
  2. The test function keeps going after Error.
  3. 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())
  1. <nil>
  2. context deadline exceeded
  3. context canceled
  4. 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

  1. cancel() closes the context's Done channel.
  2. Err() then returns context.Canceled.
  3. The second cancel() call does nothing.
  4. 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 →

Related topics

See all Go topics · All topics