GATE Computer ScienceHardGATE CS (PYQ pattern)
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 multiplications (every * executed, in all calls) does p(3, 45) perform?
- A. 9
- B. 7
- C. 12
- D. 10
Correct answer: A. 9
45 is odd (x*), 44 even (square), 22 even, 11 odd, 10 even, 5 odd, 4 even, 2 even, 1 odd, 0: counting the multiplication in every level gives 9.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- 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?
- 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?