CS FundamentalsVery hardProduct company interview · GATE level

A loop body contains an if-else followed by an independent if (with no else), and nothing else that branches. The loop runs between 0 and 3 times, depending on the input. How many distinct execution paths exist through the code, and what is its cyclomatic complexity?

  1. A. 85 paths; cyclomatic complexity 3
  2. B. 64 paths; cyclomatic complexity 4
  3. C. 85 paths; cyclomatic complexity 4
  4. D. 21 paths; cyclomatic complexity 4

Correct answer: C. 85 paths; cyclomatic complexity 4

One pass through the body can take 2 x 2 = 4 routes. With 0, 1, 2 or 3 iterations the number of paths is 1 + 4 + 16 + 64 = 85. Cyclomatic complexity counts decisions, not paths: the loop test, the if-else and the if give 3 decisions, so V(G) = 4. It is the number of linearly independent (basis) paths, which is why basis path testing needs only 4 tests although 85 paths exist. 64 counts only the three-iteration paths; 21 stops at two iterations.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic