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);- A. 7
- B. 14
- C. 0
- 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
- On a two's-complement machine, what is printed? c printf("%d %d", 40 & -40, 40 & (40 - 1));
- For a signed int n on a two's-complement machine, how many of these are correct? (i) (n & (n - 1)) == 0 is true for every positive power…
- What is printed? c #define DOUBLE(x) x + x printf("%d", 10 / DOUBLE(5) 2);
- Which of a, b, c, d are pointers? c #define IPTR int typedef int IPT; IPTR a, b; IPT c, d;
- What is printed? c #define DEBUG 0 ... #ifdef DEBUG printf("on "); #endif #if DEBUG printf("trace"); #endif
- In C, with int i = 1, a[5];, which statement has undefined behaviour?