GATE Computer ScienceVery hardGATE CS (PYQ pattern)
void swap(int *x, int *y) { *x ^= *y; *y ^= *x; *x ^= *y; } int main() { int a[5] = {3, 7, 9, 12, 5}; for (int i = 0; i < 5; i++) swap(&a[i], &a[4 - i]); for (int i = 0; i < 5; i++) printf("%d ", a[i]); } What is printed?
- A. 3 7 0 12 5
- B. 3 7 9 12 5
- C. 5 12 9 7 3
- D. 5 12 0 7 3
Correct answer: A. 3 7 0 12 5
The loop runs over the whole array, so pairs (0, 4) and (1, 3) are swapped twice (i = 0, 1 and again at i = 3, 4) and end up in their original places. At i = 2 both pointers name a[2]: *x ^= *y makes it 9 ^ 9 = 0, and the remaining XORs keep it 0. So the array is the original one with the middle zeroed. A temporary-variable swap would have left it unchanged.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- for (i = 2; i <= 100; i++) if (!comp[i]) for (j = i i; j <= 100; j += i) { comp[j] = 1; cnt++; } With comp[] initially all 0, what is the…
- long p(long x, int n) { if (n == 0) return 1; if (n % 2 == 0) { long h = p(x, n / 2); return h h; } return x p(x, n - 1); } How many…
- void perm(char s, int k) { if (s[k] == 0) { puts(s); return; } for (int i = k; s[i]; i++) { char t = s[k]; s[k] = s[i]; s[i] = t; perm(s…
- void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %dn", n); h(n - 1, c, b, a); } During…
- Bubble sort with early exit (a pass that makes no swap ends the sort, and every pass scans the whole array from index 0 to n-2) is run on…
- char s[] = "GATECS"; for (int i = 1; s[i]; i++) s[i] = 'A' + (s[i] + s[i - 1] - 2 'A') % 26; printf("%s", s); What is printed?