Given the following code, which is true: IF A > B THEN C = A – B ELSE C = A + B ENDIF Read D IF C = D Then Print “Error” ENDIF
-
1 test for statement coverage, 3 for branch coverage
-
2 tests for statement coverage, 2 for branch coverage
-
2 tests for statement coverage. 3 for branch coverage
-
3 tests for statement coverage, 2 for branch coverage
Two test cases are sufficient for both statement and branch coverage. The first test (e.g., A=10, B=5, D=5) covers the true branch of the first IF and the true branch of the second IF. The second test (e.g., A=5, B=10, D=10) covers the false branches, achieving full coverage.
To determine the number of tests required for statement coverage and branch coverage, we need to consider the different paths or branches in the code.
Let's analyze the given code:
IF A > B THEN C = A – B ELSE C = A + B ENDIF
Read D
IF C = D Then Print “Error” ENDIF
For statement coverage, we need to ensure that each statement in the code is executed at least once. In this case, we have the following statements:
- IF A > B THEN
- C = A – B
- ELSE
- C = A + B
- Read D
- IF C = D Then
- Print “Error”
To cover all the statements, we need to execute the code at least twice: once when A > B and once when A <= B. Therefore, we need 2 tests for statement coverage.
Now let's consider branch coverage. Branch coverage requires that each possible branch or decision point in the code is taken at least once. In this case, we have the following branches:
- A > B (True branch) or A <= B (False branch)
- C = D (True branch) or C != D (False branch)
To cover all the branches, we need to take both branches for the A > B decision (True and False branches) and both branches for the C = D decision (True and False branches). Therefore, we need 2 tests for branch coverage.
Therefore, the correct answer is B) 2 tests for statement coverage, 2 for branch coverage.