ProgrammingHardProduct company OA

What is printed?

What is printed?
void f(int *p, int n) {
    if (n == 0) return;
    *p += n;
    f(p, n - 1);
    *p *= 2;
}
int main(void) {
    int x = 0;
    f(&x, 3);
    printf("%d", x);
}
  1. A. 12
  2. B. 22
  3. C. 24
  4. D. 48

Correct answer: D. 48

All the additions happen on the way down (x = 3 + 2 + 1 = 6) and the three doublings on the way back up: 6 → 12 → 24 → 48. '12' doubles only once; '22' interleaves the steps as if each call added and doubled before recursing.

More C Programming questions

Exams that ask this topic