GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int x = 1; int inc(void) { return x += 1; } int dbl(void) { return x *= 2; } int sq(void) { return x *= x; } int (*fp[3])(void) = {inc, dbl, sq}; int main() { int s = 0; for (int i = 0; i < 7; i++) s += fp[(2 * i + 1) % 3](); printf("%d %d", s, x); } What is printed?
- A. 1130 722
- B. 1134 722
- C. 1134 361
- D. 1144 723
Correct answer: B. 1134 722
The index (2i+1) mod 3 for i = 0..6 is 1, 0, 2, 1, 0, 2, 1 (dbl, inc, sq, dbl, inc, sq, dbl). The successive returned values of x are [2, 3, 9, 18, 19, 361, 722], adding to 1134, and x ends as 722.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A table of size 13 uses double hashing, probe i of key k is (k mod 13 + i (1 + k mod 11)) mod 13. Keys 18, 41, 22, 44, 59, 32, 31, 73 are…
- A BST on 31 keys is perfectly balanced (all 5 levels full). Searching for each of the 31 keys with equal probability, what is the average…
- Keys 1 to 5 are inserted into an empty BST in a uniformly random order of the 120 possible orders. In how many of the orders does key 3…
- Iterative postorder uses two stacks: push the root on S1; repeat: pop x from S1, push x on S2, push x's left child then its right child on…
- struct N { int v; int next; }; struct N a[6] = {{10,3},{20,5},{30,-1},{40,1},{50,2},{60,4}}; int i = 0, s = 0, k = 1; while (i != -1) { s…
- How many structurally distinct binary search trees on keys 1..7 have key 4 at the root and height exactly 3 (a single node has height 0)?