ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int a[] = {1, 2, 4, 5, 6}, x = 0;
for (int i = 1; i <= 6; i++) x ^= i;
for (int i = 0; i < 5; i++) x ^= a[i];
printf("%d", x);
  1. A. 6
  2. B. 0
  3. C. 3
  4. D. 21

Correct answer: C. 3

XOR-ing 1 to 6 and then every element of the array cancels each number present in both, leaving only the one missing from the array: 3. Unlike the sum method, this cannot overflow.

More C Programming questions

Exams that ask this topic