GATE Computer ScienceHardGATE CS (PYQ pattern)
void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %d\n", n); h(n - 1, c, b, a); } During h(7, 'A', 'B', 'C'), which disk does the 50th printed line name, and how many of the first 49 lines name disk 1?
- A. disk 2; 25 lines
- B. disk 3; 25 lines
- C. disk 2; 24 lines
- D. disk 1; 25 lines
Correct answer: A. disk 2; 25 lines
The k-th printed move is disk 1 + (number of trailing zero bits of k), the ruler sequence of binary counting. 50 = 110010 in binary has one trailing zero, so line 50 names disk 2. Disk 1 moves on every odd-numbered line, and lines 1 to 49 contain 25 odd numbers.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- 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?
- unsigned int i, s = 0; for (i = 5; i-- > 0; ) s += i; printf("%u %u", s, i); What is printed?
- int a = 7, b = 2; float f = a / b + a / 2.0 + (float)(a / b) b; printf("%.2f %d", f, (int)(a / b 2.0)); What is printed?
- int a = 5, b = 3, c = 2; int x = a + b << c > 20; int y = 6 & 3 == 3; int z = 1 + 2 3 % 4 - 5 / 2 | 8 >> 2; printf("%d %d %d", x, y, z)…
- int C(int n, int k) { if (k == 0 || k == n) return 1; if (k == 1) return n; return C(n - 1, k - 1) + C(n - 1, k); } How many calls to C…