ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
int a = 5, b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);
  1. A. 5 3
  2. B. 6 5
  3. C. 3 6
  4. D. 3 5

Correct answer: D. 3 5

After a = a ^ b, a holds 5 ^ 3 = 6. b = 6 ^ 3 = 5 (the original a). a = 6 ^ 5 = 3 (the original b). The three XORs swap two DISTINCT variables without a temporary.

More C Programming questions

Exams that ask this topic