CS FundamentalsVery hardTCS Digital · Accenture advanced

Using the convention that each if, else-if, for, while, case, && and || adds 1 to the count of decision points, what is the cyclomatic complexity of this function? int f(int a, int b, int s) { if (a > 0 && b > 0) { for (int i = 0; i < a; i++) { if (i % 2 == 0 || b > i) s++; } } else if (a < 0) s--; while (b > 0 && s < 10) b--; return s; }

  1. A. 6
  2. B. 8
  3. C. 9
  4. D. 10

Correct answer: C. 9

Decision points: if, &&, for, if, ||, else-if, while, && = 8. Cyclomatic complexity is decision points + 1 = 9. Ignoring the short-circuit operators gives 6; forgetting the +1 gives 8.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic