ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int sumArray(int *arr, int n) {
    if (n == 0) return 0;
    return *arr + sumArray(arr + 1, n - 1);
}
int main(void) {
    int nums[] = {1, 2, 3, 4, 5};
    printf("%d", sumArray(nums, 5));
}
  1. A. 1
  2. B. 0
  3. C. 15
  4. D. 5

Correct answer: C. 15

Each recursive call adds the current element and advances the pointer by one, recursing on the remaining n-1 elements, until n reaches 0: 1+2+3+4+5=15.

More C Programming questions

Exams that ask this topic