ProgrammingHardProduct company OA

What is printed?

What is printed?
void f(int *a, int n) {
    if (n <= 1) return;
    a[n - 2] += a[n - 1];
    f(a, n - 1);
}
int main(void) {
    int a[] = {1, 2, 3, 4};
    f(a, 4);
    printf("%d %d %d %d", a[0], a[1], a[2], a[3]);
}
  1. A. 10 9 7 0
  2. B. 3 5 7 4
  3. C. 1 3 6 10
  4. D. 10 9 7 4

Correct answer: D. 10 9 7 4

Working from the right, each element adds the already-updated element after it: a[2] = 3 + 4 = 7, a[1] = 2 + 7 = 9, a[0] = 1 + 9 = 10. The result is suffix sums. '3 5 7 4' adds the original neighbours only.

More C Programming questions

Exams that ask this topic