Machine-translated by Google. Questions are authored in English.
Top

Runtime Environments and Storage Questions and Answers

1.

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?

a=3;
void n(x) {x = x * a; print(x);}
void m(y) {a = 1; a = y - a; n(a); print(a);}
void main() {m(a);}

Exam Year(s)

GATE CS - 2016,

Answer: D

The keyed output is 4, 4.

The pseudo-code has no declarations, so the key rests on reading the assignment a = 1 inside m as creating m's own local variable a (as an assignment does in many dynamically scoped languages). Under that reading:

  1. main calls m(a); y is a reference to the global a, which is 3.
  2. In m, a = 1 creates the local a; then a = y - a = 3 - 1 = 2.
  3. m calls n(a); x is a reference to m's local a. With dynamic scoping, the a inside n is the most recent active binding, m's local a, the same variable as x. So x = x * a = 2 * 2 = 4, and n prints 4.
  4. Back in m, print(a) prints the local a, now 4.

If every a were the single global variable, the output would be 0, 0 (a = 1, then a = y - a = 1 - 1 = 0), which is not offered; the options therefore assume the local reading. Under it, both prints show the same variable after n has changed it, which is why the two printed values are equal, ruling out 6, 2 and 4, 2.

Please log in or register to report this question.

Answer: C

The item stored in the heap is a dynamically allocated array of integers created using malloc().

In a C program the heap is the region managed by malloc(), calloc(), realloc() and free(). Memory obtained from malloc() stays allocated until it is freed, independent of function calls, which is exactly what the heap is for.

A static variable declared inside a function lives in the static data segment (.data or .bss), not on the heap. An ordinary array declared inside a function is an automatic variable on the stack, and the return address of a function is pushed onto the stack as part of its activation record.

Please log in or register to report this question.

Loading…
Tags: Runtime Environments and Storage Questions and Answers || Runtime Environments and Storage MCQ Questions and Answers || Runtime Environments and Storage GK Questions and Answers || Runtime Environments and Storage GK MCQ Questions || Runtime Environments and Storage Multiple Choice Questions and Answers || Runtime Environments and Storage GK || GK on Runtime Environments and Storage || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design