ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {5, 9, 12, 7};
    int p[5] = {0};
    for (int i = 0; i < 4; i++)
        p[i + 1] = p[i] ^ a[i];
    printf("%d", p[4] ^ p[1]);
}
  1. A. 7
  2. B. 2
  3. C. 5
  4. D. 12

Correct answer: B. 2

p[4] ^ p[1] equals the XOR of the elements a[1] to a[3], which is 9 ^ 12 ^ 7 = 2. Equal XOR prefixes cancel out, so the range XOR is the answer.

More C Programming questions

Exams that ask this topic