ProgrammingHardTCS NQT · Infosys pattern
What is printed? Assume 32-bit int, two's complement.
What is printed? Assume 32-bit int, two's complement.
unsigned int a = 5, b = 9;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%u %u", a, b);- A. 9 5
- B. 5 9
- C. 0 14
- D. 14 0
Correct answer: A. 9 5
This is the classic XOR-swap idiom. After the three XOR steps a and b end up swapped without a temp: a becomes 9 (old b) and b becomes 5 (old a).
More C Programming questions
- What is printed? c int flag = 0, mask = 2; flag |= mask == 2; printf("%d", flag);
- What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; for (int i = 0; i < 3; i++) { (p + i) = 10; }…
- What is printed? (undefined behavior aside, assume typical stack layout where locals of adjacent calls can overlap after return) c int…
- What is printed? c char arr[] = {"one", "two", "three"}; char p = arr; printf("%s %c %s", p, ((p + 1) + 1), p[2]);
- What is printed? Assume 32-bit int. c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);
- What can be said about this program? c #include <stdlib.h> int main(void) { int p = malloc(sizeof(int)); free(p); free(p); }