CS FundamentalsVery hardProduct company interview · GATE level

The tests (x = 3, y = 4) and (x = -2, y = 7) are run on this function. What statement, branch (decision) and condition coverage do they achieve? (Condition coverage counts the true and false outcomes of each of the three simple conditions; and short-circuits.)

The tests (x = 3, y = 4) and (x = -2, y = 7) are run on this function. What statement, branch (decision) and condition coverage do they achieve? (Condition coverage counts the true and false outcomes of each of the three simple conditions; and short-circuits.)
def f(x, y):
    r = 0
    if x > 0:
        r = 1
    if y > 0 and x < 5:
        r = r + 2
    else:
        r = r - 1
    return r
  1. A. Statement 80%, branch 75%, condition 100%
  2. B. Statement 100%, branch 75%, condition 67%
  3. C. Statement 80%, branch 100%, condition 67%
  4. D. Statement 80%, branch 75%, condition 67%

Correct answer: D. Statement 80%, branch 75%, condition 67%

Count the five simple statements r = 0, r = 1, r = r + 2, r = r - 1 and return r. Both tests make the second decision true, so r = r - 1 never runs: 4/5 = 80%. Branches: the first if goes both ways (3 > 0, -2 > 0 false), the second only to its true side: 3 of 4 outcomes, 75%. Conditions: x > 0 is seen true and false, but y > 0 and x < 5 are only ever true: 4 of 6 outcomes, 67%. A test such as (x = 6, y = 1) would add the missing false outcomes.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic