Skip to content
Official PYQ · 1989

GATE CS 1989 Q3-viii — Runtime Environments

Explain with AI

In which of the following case(s) is it possible to obtain different results for call-by-reference and call-by-name parameter passing?

Select all correct options
View worked solution

Answer: A;D

To understand when call-by-reference and call-by-name produce different results, let us look at how they handle parameter passing:

  • Call-by-Reference: The address of the actual parameter is computed once at the time of the function call and passed to the formal parameter.
  • Call-by-Name: The actual parameter is textually substituted into the function body. The expression is re-evaluated every time the formal parameter is accessed during execution.

Let's analyze the options:

  1. Passing an expression as a parameter (Option A):
    Suppose we pass an expression like x+1x + 1 as a parameter to a function, and the value of xx is modified inside the function.
    • In call-by-reference, x+1x + 1 is evaluated once before the call, stored in a temporary location, and its address is passed. Changes to xx inside the function do not affect this temporary value.
    • In call-by-name, the expression x+1x + 1 is re-evaluated every time the parameter is accessed. If xx changes inside the function, the parameter's value changes accordingly.
    Thus, they can produce different results.

  2. Passing an array element as a parameter (Option D):
    Suppose we pass an array element A[i]A[i] to a function, and the index variable ii is modified inside the function.
    • In call-by-reference, the address of A[i]A[i] is computed once at the start of the call. Even if ii changes inside the function, the parameter still points to the original array element.
    • In call-by-name, the expression A[i]A[i] is substituted. If ii changes inside the function, subsequent accesses to the parameter will refer to the new element A[extnewi]A[ ext{new } i] (this is known as Jensen's Device).
    Thus, they can produce different results.

For simple arrays (Option B) or pointers (Option C), there are no dynamic index variables or expressions to re-evaluate, so both mechanisms yield the same behavior.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions