GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int i = 1; int A[1..4] = {4, 7, 2, 9}; void swap(x, y) { int t = x; x = y; y = t; } The call swap(i, A[i]) is made. The parameters are passed by name (each use of x or y re-evaluates the argument expression in the caller's environment). What are the values after the call?
- A. i = 4; A[1..4] = 1, 7, 2, 9
- B. i = 4; A[1..4] = 4, 7, 2, 1
- C. i = 1; A[1..4] = 4, 7, 2, 9
- D. i = 4; A[1..4] = 4, 7, 2, 9
Correct answer: B. i = 4; A[1..4] = 4, 7, 2, 1
By name: t = i = 1; then i = A[i] = A[1] = 4; then A[i] = t becomes A[4] = 1 (i is now 4). With reference the second store would go to A[1], the element chosen at call time.
More Compiler Design questions
- For S′ → S; S → A | x b; A → a A b | B; B → x, what is the LALR(1) lookahead set of the item B → x · in the state reached from the start…
- int x = 1; int a() { return x; } int b(int n) { int x = n 10; if (n == 0) return a(); return b(n - 1) + a(); } What does b(2) return under…
- A basic block is converted to a DAG with value numbering (+ and commutative; a new leaf is made for each variable's initial value; a…
- A basic block has 8 three-address statements, of which 3 compute a triple identical to an earlier one. A compiler stores it as (i)…
- In 1: a = 1; 2: b = 2; 3: if p > 0 goto L1; 4: a = a + 1; 5: goto L2; 6: L1: b = b + 1; 7: L2: c = a + b; 8: if c > q goto L3; 9: a = c…
- In the loop 1: i = 0; 2: j = 5; 3: L1: t1 = i 4; 4: t2 = t1 + b; 5: j = j + 3; 6: k = j 2; 7: m = m 2; 8: u = i + n; 9: i = i + 1; 10: if…