ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
void swap(int *x, int *y) {
    *x ^= *y;
    *y ^= *x;
    *x ^= *y;
}
...
int a = 7;
swap(&a, &a);
printf("%d", a);
  1. A. 7
  2. B. 14
  3. C. 0
  4. D. Compilation error

Correct answer: C. 0

Both pointers refer to the same int. The first line computes a ^= a, which is 0, and every later XOR of 0 with 0 stays 0. The XOR swap fails when both arguments alias; a temporary-variable swap would leave 7.

More C Programming questions

Exams that ask this topic