ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
int arr[5] = {1, 2, 3, 4, 5};
int *start = arr;
int *end = arr + 5;
int sum = 0;
for (int *p = start; p != end; p++) {
    sum += *p;
}
printf("%d", sum);
  1. A. 15
  2. B. 10
  3. C. Undefined behaviour: end points one past the array
  4. D. 0

Correct answer: A. 15

arr + 5 is exactly one-past-the-end of a 5-element array, which C explicitly permits forming (just never dereferencing). The loop walks p from start to (but not including) end, visiting all 5 real elements and summing them: 1+2+3+4+5=15.

More C Programming questions

Exams that ask this topic