GATE Computer ScienceHardGATE CS
int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int main() { int (*op[3])(int, int) = { add, sub, mul }; int r = 2; for (int i = 0; i < 8; i++) r = op[(i * i) % 3](r, i + 1); printf("%d", r); } What is printed?
- A. 11
- B. −7
- C. 12
- D. −10
Correct answer: D. −10
i² mod 3 is 0 for i divisible by 3 and 1 otherwise, so the operations used are add for i = 0, 3, 6 and sub for the others; mul is never selected. r: 2 → 3 → 1 → −2 → 2 → −3 → −9 → −2 → −10.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Hoare partition on a[0..8] = {8, 3, 9, 1, 8, 5, 2, 7, 6}: int hoare(int a[], int lo, int hi) { int p = a[lo], i = lo - 1, j = hi + 1…
- A singly linked list has a tail of 6 nodes leading into a cycle of 9 nodes. A pointer 'slow' moves 1 node per step and a pointer 'fast'…
- An open-addressing table of size 11 uses h(k) = k mod 11 with linear probing and tombstones. Insertion places the key in the first empty…
- A sparse table for range-minimum queries on an array of 1000 elements stores, for each level k = 0, 1, ..., ⌊log₂ 1000⌋, one entry for…
- A segment tree over positions 0 to 12 (13 elements) is built recursively: the root is node 1, the node for [lo, hi] splits at mid = (lo +…
- A B-tree with minimum degree t = 3 (every node except the root has between 2 and 5 keys) is built with the CLRS insertion procedure, which…