CS FundamentalsVery hardProduct-company placement · GATE CS

The function below has cyclomatic complexity 3. What is the minimum number of test calls that achieves 100% branch coverage of it?

The function below has cyclomatic complexity 3. What is the minimum number of test calls that achieves 100% branch coverage of it?
def g(n):
    steps = 0
    while n != 1:
        if n % 2 == 0:
            n //= 2
        else:
            n = 3 * n + 1
        steps += 1
    return steps
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: C. 1

g(3) runs 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1, taking the odd branch, the even branch, the loop body and finally the loop exit, so one test covers all four outcomes. Cyclomatic complexity is an upper bound on the tests branch coverage needs, not the minimum.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic