CS FundamentalsVery hardWipro · Accenture pattern

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 classify(n):
    if n < 0:
        return "neg"
    elif n == 0:
        return "zero"
    elif n < 10:
        return "small"
    else:
        return "big"
  1. A. 5
  2. B. 2
  3. C. 4
  4. D. 3

Correct answer: C. 4

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

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic