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. 1 test for statement coverage, 3 for branch coverage

  2. 2 tests for statement coverage, 2 for branch coverage

  3. 2 tests for statement coverage. 3 for branch coverage

  4. 3 tests for statement coverage, 2 for branch coverage


Correct Option: B

AI Explanation

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:

  1. IF A > B THEN
  2. C = A – B
  3. ELSE
  4. C = A + B
  5. Read D
  6. IF C = D Then
  7. 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:

  1. A > B (True branch) or A <= B (False branch)
  2. 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.

Find more quizzes: