33,390 views
56 56 votes

Which of the following are true?

  1. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation

  2. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions

  3. Recursion in programming languages cannot be implemented with dynamic storage allocation

  4. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records

  5. Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records

  1. II and V only
  2. I, III and IV only
  3. I, II and V only
  4. II, III and V only

8 Answers

Best answer
48 48 votes
  1. False. Recursion cannot be implemented using static allocation.
  2. True. Yes, we do need multi level access link in case of nested functions. Each level to traverse ARB of same level of nesting.
  3. False. Recursion can only be implemented using dynamic memory allocation.
  4. False. Recursion is done using memory in stack (ARBs in stack), not in heap.
  5. True. Yes, they cannot, once a function returns its activation record is no longer valid, so we cannot return a function as a result.


So, option (A) is correct.

edited by
50 50 votes

 

Answer: A

1. False — Recursion cannot be implemented using static storage allocation.

In static storage allocation, a fixed amount of memory is reserved at compile time for each parameter and local variable of a function. Since no additional storage is provided for repeated calls, intermediate values from nested recursive calls cannot be preserved. Hence, systems with only static storage allocation cannot support recursion.
factorial (static int n)
{
    if (n <= 1)
        return 1;
    else
        return (n * fact(n - 1));
}

If this program is executed for any value of \( n \), say \( n = 4 \), then \( \text{fact}(4) \) will always return \( 1 \).

2. True

Explanation: See the detailed discussion here.

3. False

Recursion is implemented using a runtime stack, which is part of the dynamic storage area. The memory layout during execution typically includes two dynamically growing regions — the stack and the heap, which grow in opposite directions.

The stack is used for function activation records and supports recursion by maintaining each call’s context separately.

4. False — Same reasoning as above.

5. True

Consider the following Python code, where the function \( a() \) returns another function \( b() \):

def a():
    print("A!")

    def b():
        print("B!")

    return b

s = a()   # a() returns the function b
s()       # now call b()
This behavior is not possible using only a stack. Once the activation record of \( a() \) is popped from the runtime stack, the nested function \( b() \), defined inside \( a() \), would no longer exist. However, since \( a() \) returns \( b() \), we must retain its reference even after \( a() \) completes — which requires dynamic (heap-based) storage for closures and function objects.
edited by
14 14 votes
Answer is A.

I. Recursion can never be implemented with Static Storage Allocation.

II, Is TRUE.  

III. Recursion can be implemented with Dynamic Storage Allocation but not with Static Storage Allocation.

IV. Can be done with Stack based allocation scheme.

V. Is TRUE as with a stack based allocation once a function returns its activation record is no longer valid- so we cannot return a function as a result.
1 1 vote

option- a

I.  Recursion cannot be implemented with Static Storage Allocation.   Static allocation means, compiler has to decide size for function calls.  In case of recursion, it is not possible for compiler to decide as depth of recursion depends on recursion parameter which may be an input from user also.

II. Is CORRECT.  Programming languages that support nested subroutines also have a field in the call frame that points to the stack frame of the latest activation of the procedure that most closely encapsulates the callee, i.e. the immediate scope of the callee. This is called an access link or static link (as it keeps track of static nesting during dynamic and recursive calls) and provides the routine (as well as any other routines it may invoke) access to the local data of its encapsulating routines at every nesting level. Some architectures, compilers, or optimization cases store one link for each enclosing level (not just the immediately enclosing), so that deeply nested routines that access shallow data do not have to traverse several links; this strategy is often called a “display”

III. Recursion CAN be implemented with any kind of  Dynamic Storage Allocation scheme.

IV. Nesting features are always implemented in a language using STACK and NOT Heap. (See above point II for details)

V. Is CORRECT.  In stack based allocation scheme, once a function has returned, it is removed from function call stack.  Therefore returning a function from a function doesn’t look possible.

1 1 vote

Explanation: I.  Recursion cannot be implemented with Static Storage Allocation.   Static allocation means, compiler has to decide size for function calls.  In case of recursion, it is not possible for compiler to decide as depth of recursion depends on recursion parameter which may be an input from user also.

II. Is CORRECT.  Programming languages that support nested subroutines also have a field in the call frame that points to the stack frame of the latest activation of the procedure that most closely encapsulates the callee, i.e. the immediate scope of the callee. This is called an access link or static link (as it keeps track of static nesting during dynamic and recursive calls) and provides the routine (as well as any other routines it may invoke) access to the local data of its encapsulating routines at every nesting level. Some architectures, compilers, or optimization cases store one link for each enclosing level (not just the immediately enclosing), so that deeply nested routines that access shallow data do not have to traverse several links; this strategy is often called a “display”  [Source:  https://en.wikipedia.org/wiki/Call_stack ]

III. Recursion CAN be implemented with any kind of  Dynamic Storage Allocation scheme.

IV. Nesting features are always implemented in a language using STACK and NOT Heap. (See above point II for details)

V. Is CORRECT.  In stack based allocation scheme, once a function has returned, it is removed from function call stack.  Therefore returning a function from a function doesn’t look possible.

source:_https://www.geeksforgeeks.org/gate-gate-cs-2008-question-54/

1 1 vote

We read option I - it's false.

So we hv to decide between only A and D and to do that we need to determine whether III is true or false.

III is false so option A is the ans.

Answer:
Position:
Show:

Related questions

74 74 votes
4 answers 4 answers
35.1k
35.1k views
Kathleen asked Sep 12, 2014
35,095 views
Which of the following are NOT true in a pipelined processor?Bypassing can handle all RAW hazardsRegister renaming can eliminate all register carried WAR hazardsControl h...
48 48 votes
3 answers 3 answers
26.5k
26.5k views
Kathleen asked Sep 12, 2014
26,531 views
The use of multiple register windows with overlap causes a reduction in the number of memory accesses for:Function locals and parametersRegister saves and restoresInstruc...
60 60 votes
6 answers 6 answers
17.9k
17.9k views
Kathleen asked Sep 12, 2014
17,929 views
The subset-sum problem is defined as follows. Given a set of $n$ positive integers, $S = \{ a_1, a_2, a_3, \dots , a_n \}$, and positive integer $W$, is there a subset of...
49 49 votes
3 answers 3 answers
25.3k
25.3k views
Kathleen asked Sep 12, 2014
25,295 views
An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only ifThe SLR(1) parser for G has S-R conflictsThe LR(1) parser for G has S-R conflictsThe...