Correct Answer: D
For the code fragment given below, one test case is required for statement coverage and two test cases are required for branch coverage. Statement coverage requires that every executable statement in the code is executed at least once. Branch coverage requires that every possible outcome of each decision point in the code is executed at least once. The following table shows how to achieve statement and branch coverage for the code fragment:
Test Case
Input
Output
Statement Coverage
Branch Coverage
1
x = 3
y = 9
S1, S2, S3
T1 = true
2
x = 5
y = 25
S1, S2
T1 = false
As shown in the table, test case 1 covers all three statements (S1, S2, S3) in the code fragment, achieving
100% statement coverage. Test case 2 covers only two statements (S1, S2) in the code fragment, but it exercises a different outcome of the decision point T1 (if x < 4), achieving 100% branch coverage.