ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {1, 2, 3, 4, 5, 6};
    int *l = a, *r = a + 5;
    while (l < r) {
        int t = *l;
        *l++ = *r;
        *r-- = t;
    }
    for (int i = 0; i < 6; i++) printf("%d", a[i]);
}
  1. A. 654321
  2. B. 615243
  3. C. 123456
  4. D. 654123

Correct answer: A. 654321

Each iteration swaps the two ends and moves both pointers inward, so the array is reversed in place. Forgetting to move the pointers after the swap would give a different pattern.

More C Programming questions

Exams that ask this topic