Choose the best reason why testing is important when writing software.
Think about what happens if bugs are found after users start using the software.
Testing helps catch mistakes early so they can be fixed before the software reaches users, improving quality and reliability.
Look at this Go test code. What will it print when run?
package main import ( "testing" ) func Add(a, b int) int { return a + b } func TestAdd(t *testing.T) { result := Add(2, 3) if result != 5 { t.Errorf("Expected 5 but got %d", result) } else { println("Test passed") } }
Check what the Add function returns and what the test expects.
The Add function returns 5 for inputs 2 and 3, so the test prints "Test passed".
What error message will this Go test produce when run?
package main import "testing" func Divide(a, b int) int { return a / b } func TestDivide(t *testing.T) { _ = Divide(10, 0) }
Think about what happens when dividing by zero in Go.
Dividing by zero causes a runtime panic error in Go, stopping the program.
Choose the option that is NOT a benefit of writing tests for your code.
Think about what testing can and cannot do for your code.
Testing helps find bugs and verify behavior but does not automatically improve how readable the code is.
Given this Go test code, how many test cases will run?
package main import "testing" func IsEven(n int) bool { return n%2 == 0 } func TestIsEven(t *testing.T) { tests := []struct { input int expect bool }{ {2, true}, {3, false}, {0, true}, {-4, true}, } for _, test := range tests { if result := IsEven(test.input); result != test.expect { t.Errorf("For %d expected %v but got %v", test.input, test.expect, result) } } }
Count how many test cases are defined in the tests slice.
The tests slice contains 4 test cases, so the loop runs 4 times.