CS FundamentalsVery hardProduct-company placement · TCS Digital

For the function below, which single call executes every statement (100% statement coverage)?

For the function below, which single call executes every statement (100% statement coverage)?
def f(a, n):
    total = 0
    for x in a:
        if x < 0:
            continue
        if x > n and x % 2 == 0:
            total += x
        elif x == n:
            break
    return total
  1. A. f([8, 5, -1], 5)
  2. B. f([-1, 8, 5], 5)
  3. C. f([-1, 7, 5], 5)
  4. D. f([5, -1, 8], 5)

Correct answer: B. f([-1, 8, 5], 5)

[-1, 8, 5] hits continue (-1), total += x (8 is > 5 and even) and then break (5). In [8, 5, -1] and [5, -1, 8] the loop breaks at 5 before reaching -1 or 8; 7 is odd, so total += x never runs.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic