CS FundamentalsVery hardProduct-company placement · GATE CS

Counting one decision for each loop, if, else-if, && or || operator, case label (not default) and catch clause, what is the cyclomatic complexity of this fragment?

Counting one decision for each loop, if, else-if, && or || operator, case label (not default) and catch clause, what is the cyclomatic complexity of this fragment?
for (i = 0; i < n; i++) {
  if (a[i] > 0 && b[i] > 0) p++;
  else if (a[i] < 0) q++;
}
switch (k) {
  case 1: case 2: x = 1; break;
  case 3: x = 2; break;
  default: x = 0;
}
try { r = risky(); }
catch (IOException e) { r = -1; }
catch (ParseException e) { r = -2; }
  1. A. 11
  2. B. 7
  3. C. 10
  4. D. 4

Correct answer: C. 10

Decisions: for 1, if 1, && 1, else-if 1, case labels 1, 2 and 3 (3), two catches 2: total 9, so V(G) = 10. Counting default gives 11, ignoring the && and the catches gives 7, and 4 counts each statement (for, if, switch, try) once.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic