ProgrammingHardWipro · Capgemini pattern
int x = 7; int *a = &x, *b = &x; *a ^= *b; *b ^= *a; *a ^= *b; What is x afterwards?
- A. 0
- B. 14
- C. -7
- D. undefined
Correct answer: A. 0
The first XOR makes x equal 0, and every later XOR of 0 with 0 leaves it at 0. Both pointers refer to the same int, so these modifications are well defined.
More C Programming questions
- int a[] = {5, 1, 4}; int p = a; p++ += 10; printf("%d %d", a[0], p); prints what?
- struct P { int id; int score; }; An array must be sorted by score descending, with ties broken by id ascending, using qsort. Which…
- For a = {2, 4, 2, 2, 6, 4}, how many index pairs i < j satisfy a[i] == a[j]?
- uint32t fact(uint32t n) { return n <= 1 ? 1 : n fact(n - 1); } What does fact(13) return?
- uint8t acc = 0; uint8t a[3] = {250, 3, 7}; for each element, acc += a[i]; What is acc after all three additions?
- int a[] = {3, 5, 6, 9}; int pre[5]; pre[0] = 0, and pre[i] = pre[i-1] ^ a[i-1] for i = 1..4. What is pre[4] ^ pre[1]?