CS FundamentalsVery hardProduct company interview · GATE level

Counting each simple condition in a compound boolean expression as a separate decision, what is the cyclomatic complexity of this function?

Counting each simple condition in a compound boolean expression as a separate decision, what is the cyclomatic complexity of this function?
def scan(items):
    total = 0
    for x in items:
        if x % 2 == 0:
            total += x
        else:
            total -= x
    return total
  1. A. 1
  2. B. 3
  3. C. 2
  4. D. 4

Correct answer: B. 3

Counting decision points: 2 (each if/elif/while/for test, and each 'and'/'or' as a separate condition). Cyclomatic complexity = decisions + 1 = 2 + 1 = 3.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic