CS FundamentalsVery hardTCS NQT · Infosys 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 search(arr, key):
    i = 0
    while i < len(arr):
        if arr[i] == key:
            return i
        i += 1
    return -1
  1. A. 2
  2. B. 4
  3. C. 1
  4. D. 3

Correct answer: D. 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