ProgrammingHardProduct company OA

What is printed?

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

Correct answer: B. 9

XOR is associative and commutative, and v ^ v = 0, so every value appearing an even number of times cancels out. 4 and 7 appear twice; 9 appears three times, leaving one 9.

More C Programming questions

Exams that ask this topic