CS FundamentalsVery hardProduct company interview · GATE level

What is the cyclomatic complexity of this fragment? Count each case label other than default as one decision and each simple condition separately.

What is the cyclomatic complexity of this fragment? Count each case label other than default as one decision and each simple condition separately.
switch (x) {
    case 1: a();          /* falls through */
    case 2: b(); break;
    case 3: c(); break;
    default: d();
}
while (k > 0 && !done) {
    k--;
    if (k == 5) continue;
    log(k);
}
  1. A. 8
  2. B. 7
  3. C. 16
  4. D. 11

Correct answer: B. 7

The switch has 3 non-default case decisions (the fall-through adds an edge, not a decision), the loop condition 2 and the if 1: 6 decisions, V(G) = 7. Counting default too gives 8; 16 and 11 are the edge and node counts of the flow graph (16 − 11 + 2 = 7).

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic