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.

3.

Procedure activation record must contain :

(i) Space for arguments
(ii) Space for local data and local temporaries
(iii) Space for global data

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: D

The activation record must contain (i) and (ii), so the answer is (i) and (ii) only.

An activation record (stack frame) is created for each call of a procedure. It holds the actual parameters (arguments), the returned value, the control link and access link, the saved machine status such as the return address, and space for local data and temporaries.

Global data is not stored there: it lives in the static data area, allocated once for the whole program and shared by all procedures. Options that include (iii) are therefore wrong, and '(ii) only' leaves out the arguments.

Please log in or register to report this question.

4.

Which of the following parameter passing methods works with the idea that the argument is not evaluated until its actual use as a parameter in the called program ?

Exam Year(s)

MH SET - 2023, SET Exam - 2023,

Answer: B

The method is Pass by Name.

In call by name, introduced in Algol 60, the actual argument is not evaluated at the call. It is passed unevaluated (in practice as a small routine, a thunk), and re-evaluated in the caller's environment each time the parameter is used in the called procedure. This is the idea behind lazy evaluation.

Pass by value evaluates the argument once at the call and copies the value. Pass by reference passes the address of an already-evaluated variable. Pass by value-result copies the value in at the call and copies it back on return.

Please log in or register to report this question.

Answer: B

Activation records are stored in the call stack, representing the sequence of function calls.

Each time a function is called, an activation record (stack frame) holding its parameters, return address, saved registers and local variables is pushed onto the call stack; it is popped when the function returns. The stack of frames therefore records the chain of active calls, which also makes recursion possible.

Activation records are not normally kept in the heap or managed globally, the call stack is not for loop constructs, and the two concepts are closely related, not unrelated.

Please log in or register to report this question.

Loading…

Other Subjects

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