MCQs on Testing and Benchmarking | Go

In Go programming, writing unit tests, conducting table-driven tests, and performing benchmarking and profiling are essential practices to ensure efficient, reliable, and performant code. Mastering these concepts leads to robust software development.


Topics and MCQs

1. Writing Unit Tests (testing Package)

  1. Which package is used to write unit tests in Go?
    a) testing
    b) unit
    c) go-test
    d) test
  2. What is the function used to run a unit test in Go?
    a) TestRun()
    b) Test()
    c) RunTest()
    d) Execute()
  3. Which Go function is used to assert equality in unit tests?
    a) assertEqual()
    b) assert()
    c) Equal()
    d) t.Errorf()
  4. What is the method to indicate that a test has failed in Go?
    a) t.Fail()
    b) t.Pass()
    c) t.Error()
    d) t.FailNow()
  5. Which command is used to run tests in Go?
    a) go test
    b) go run
    c) go test –run
    d) go build
  6. What does the t.Log() function do in Go testing?
    a) Logs a message to the standard output
    b) Starts a new test log
    c) Prints the results of the test
    d) Ends the current test
  7. Which of the following is the correct signature for a unit test function in Go?
    a) func TestFunction(t *testing.T) {}
    b) func UnitTest(t *testing.T) {}
    c) func TestFunction(t *unit.Test) {}
    d) func runTest(t *testing.T) {}
  8. How can you skip a test in Go?
    a) t.SkipNow()
    b) t.Skip()
    c) t.Ignore()
    d) t.Pause()
  9. Which method is used to log an error during a test?
    a) t.Fatal()
    b) t.LogError()
    c) t.Error()
    d) t.FailNow()
  10. What does the t.Parallel() function do in Go testing?
    a) It runs tests sequentially
    b) It runs tests concurrently
    c) It logs results parallelly
    d) It skips the test if parallel

2. Table-Driven Tests

  1. What is a table-driven test in Go?
    a) A test where different inputs are tested using a loop
    b) A test with fixed input data
    c) A test that runs without assertions
    d) A test with multiple test functions
  2. How do you define a table-driven test in Go?
    a) By using structs and a loop to test multiple cases
    b) By using a single function to define all test cases
    c) By writing separate functions for each case
    d) By using a custom testing framework
  3. What is the role of struct in table-driven tests?
    a) It holds the test input and expected output
    b) It stores the test functions
    c) It helps log errors
    d) It defines the test cases’ structure
  4. In a table-driven test, which keyword is used to iterate over the test cases?
    a) forEach
    b) for
    c) range
    d) loop
  5. How does a table-driven test improve test coverage in Go?
    a) By running the same test for multiple data inputs
    b) By using random values for testing
    c) By testing only boundary conditions
    d) By splitting tests into multiple files
  6. How do you define the expected result in a table-driven test?
    a) By using a function that returns a value
    b) By comparing actual results to an expected value
    c) By setting a variable
    d) By logging the result after the test
  7. Which function is typically used in a table-driven test to compare actual and expected values?
    a) t.Assert()
    b) t.Compare()
    c) t.Errorf()
    d) t.Equal()
  8. What does the t.Run() function do in a table-driven test?
    a) Runs a subtest for each table entry
    b) Executes the entire test suite
    c) Skips the test
    d) Logs the test results
  9. What happens if a table-driven test case fails in Go?
    a) Only the failing case is logged
    b) All subsequent test cases are skipped
    c) The test suite terminates immediately
    d) The function continues to the next test case
  10. How do you ensure each test case in a table-driven test runs independently in Go?
    a) By using subtests with t.Run()
    b) By separating test cases into different functions
    c) By using a global variable
    d) By skipping previous test cases

3. Benchmarking and Profiling

  1. Which package is used for benchmarking in Go?
    a) benchmark
    b) testing
    c) profiler
    d) testbench
  2. What is the function signature for a benchmark test in Go?
    a) func BenchmarkTest(b *testing.B) {}
    b) func Benchmark(b *testing.B) {}
    c) func TestBenchmark(b *testing.B) {}
    d) func benchmark(b *testing.B) {}
  3. Which method is used to run benchmarks in Go?
    a) b.Run()
    b) b.Start()
    c) b.N()
    d) b.Execute()
  4. What does the b.N variable represent in a benchmark function?
    a) The number of iterations for the benchmark
    b) The number of successful runs
    c) The benchmark’s expected result
    d) The time taken to complete the benchmark
  5. Which of the following is true about the testing.B type in Go?
    a) It is used to benchmark functions, measuring execution time
    b) It is used for logging benchmark results
    c) It is used to assert test failures in benchmarking
    d) It is used to define the expected output for benchmarks
  6. How do you run benchmarks in Go from the command line?
    a) go run benchmark_test.go
    b) go test -bench .
    c) go benchmark .
    d) go test –bench-mark
  7. What is the purpose of Go’s pprof tool?
    a) To visualize memory usage
    b) To profile CPU and memory consumption
    c) To run benchmarks
    d) To manage test results
  8. Which of the following is a profiling tool available in Go?
    a) pprof
    b) benchprof
    c) testprof
    d) goprof
  9. How do you profile CPU usage in Go?
    a) By using pprof.StartCPUProfile()
    b) By using pprof.StartMemoryProfile()
    c) By running benchmarks only
    d) By manually calculating CPU usage
  10. Which of the following is used to generate a heap profile in Go?
    a) pprof.StartHeapProfile()
    b) pprof.WriteHeapProfile()
    c) pprof.StartCPUProfile()
    d) pprof.GenerateHeapProfile()

Answers Table

QNoAnswer (Option with Text)
1a) testing
2b) Test()
3d) t.Errorf()
4c) t.Error()
5a) go test
6a) Logs a message to the standard output
7a) func TestFunction(t *testing.T) {}
8b) t.Skip()
9c) t.Error()
10b) It runs tests concurrently
11a) A test where different inputs are tested using a loop
12a) By using structs and a loop to test multiple cases
13a) It holds the test input and expected output
14c) range
15a) By running the same test for multiple data inputs
16b) By comparing actual results to an expected value
17c) t.Errorf()
18a) Runs a subtest for each table entry
19a) Only the failing case is logged
20a) By using subtests with t.Run()
21b) testing
22b) func Benchmark(b *testing.B) {}
23c) b.N()
24a) The number of iterations for the benchmark
25a) It is used to benchmark functions, measuring execution time
26b) go test -bench .
27b) To profile CPU and memory consumption
28a) pprof
29a) By using pprof.StartCPUProfile()
30b) pprof.WriteHeapProfile()

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