CS FundamentalsVery hardProduct company interview · GATE level

Counting every simple condition of a compound boolean expression as a separate decision (each && / || adds one), what is the cyclomatic complexity of this function?

Counting every simple condition of a compound boolean expression as a separate decision (each && / || adds one), what is the cyclomatic complexity of this function?
def total(a, n):
    s = 0
    i = 0
    while i < n:
        if a[i] < 0 and i % 2 == 0:
            break
        elif a[i] == 0 or a[i] > 100:
            i += 1
            continue
        s += a[i]
        i += 1
    return s
  1. A. 6
  2. B. 4
  3. C. 10
  4. D. 14

Correct answer: A. 6

Predicates: the while test (1), a[i] < 0 and i % 2 == 0 (2), a[i] == 0 or a[i] > 100 (2): 5 simple decisions, so V(G) = 6; break and continue add edges, not decisions. Treating each if/elif as one decision gives 4, and 10 and 14 are the node and edge counts, not E − N + 2.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic