CS FundamentalsHardProduct-company placement · Infosys SP

Counting each simple condition separately (each 'and'/'or' adds one) and each loop header as one decision, what is the cyclomatic complexity of this function?

Counting each simple condition separately (each 'and'/'or' adds one) and each loop header as one decision, what is the cyclomatic complexity of this function?
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. 5
  2. B. 3
  3. C. 6
  4. D. 1

Correct answer: C. 6

Decisions: for (1), x < 0 (1), x > n and x % 2 == 0 (2), x == n (1) = 5, so V(G) = 6. Counting the compound condition as one gives 5, counting only the three if/elif tests gives 3, and 1 treats the function as a single path.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic