All mock tests are FREE till 31 December — register to save your progress

Consider the function in C code:
Cal(a, b) {
  if (b != 1) {
    if (a != 1) {
      printf("*");
      Cal(a/2, b);
    }
    else {
      b = b - 1;
      Cal(10, b);
    }
  }
}
How many times will * be printed if the function is called with Cal(10, 10)?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. 27

Explanation

For a fixed b ≠ 1, starting from a = 10 the calls print * for a = 10, 5 and 2 (3 stars), then a = 1 decrements b and restarts with a = 10. This happens for b = 10, 9, …, 2, i.e. 9 times, giving 9 × 3 = 27 stars. When b becomes 1 the recursion stops.

More Programming Languages and Computer Graphics questions