in Compiler Design edited by
12,923 views
49 votes
49 votes

Which of the following statements are CORRECT?

  1. Static allocation of all data areas by a compiler makes it impossible to implement recursion. 
  2. Automatic garbage collection is essential to implement recursion. 
  3. Dynamic allocation of activation records is essential to implement recursion. 
  4. Both heap and stack are essential to implement recursion. 
  1. $1$ and $2$ only
  2. $2$ and $3$ only
  3. $3$ and $4$ only
  4. $1$ and $3$ only
in Compiler Design edited by
12.9k views

4 Comments

None of the answer given below are clear.

Even the best answer is using answer elimination.
2
2
Recursion can not be implemented without stack. Heap is not needed in recursion if there is no use of dynamic memory allocation like pointers.
0
0

Answer: (D)

Explanation: 1) Static allocation of all data areas by a compiler makes it impossible to implement recursion.
True, dynamic allocate of memory is required for function call stack as number of calls is not known advance for recursive functions.

2) Automatic garbage collection is essential to implement recursion.
False, Automatic garbage collection is not essential.
 

3) Dynamic allocation of activation records is essential to implement recursion.
True, as number of calls or number of activation records is not known advance for recursive functions.

4) Both heap and stack are essential to implement recursion.
Heap is not needed for function calls. It is generally used for dynamic memory allocation by user (or programmer).

 

Source: https://www.geeksforgeeks.org/gate-gate-cs-2014-set-3-question-28/

 

14
14

@smsubham Explanation for $4$ is not apt. 

It is generally used for dynamic memory allocation by user (or programmer).

Recursion does require dynamic memory allocation.

We can even implement recursion with heap. Any of stack/heap (dynamic storage allocation schemes) can implement recursion.

4
4

3 Answers

42 votes
42 votes
Best answer

It will be D.

option $2$ is wrong because it is not necessary to have automatic garbage collection to implement recursion.

option $4$ is wrong because it says that both are required to implement recursion, which is wrong. Either of them will suffice.

edited by

4 Comments

@arjun sir, with this " Actually it is not possible. Recursion needs a stack structure. It can be done using a heap, but then we have to simulate s stack using the heap" statement, given by you,

can we say that option 4 would also be correct if it we are asked about only "recursion need heap and stack both in case if we are trying to implement recursion using heap "?

1
1
If option 3 is correct, then Heap is required for dynamic memory allocation, which makes option 4 correct since a stack is necessarily required for recursion.

No options match to the above proposition.
1
1

@Abhilash Behera you are wrong bro because stack region is also static.

0
0
24 votes
24 votes

1) In Static allocation of memory , we cannot implement recursion. As in recursion we cannot determine at first how many time function will be called

2)Automatic garbage collection done in JAVA (JVM done this) and LISP. But recursion we can implement in C too.So, Automatic Garbage Collection is not essential for Recursion

3) True. Same reason as 1)

4)Stack implement recursion, but heap is not necessary for recursion

4 Comments

@arjun sir if one of the option is given as 1,3,4 then we click on this option ?? in exam plz reply
0
0
@rajan statement 4 is false because either of them (heap or stack) is sufficient to implement recursion.
1
1
@akash.dinkar12 Option 1 says static allocation ie stack is imp , Option 2 says heap is also important ie dynamic allocation is also imp .So both of them are needed making Option 4 right. Am i right?
0
0

Statement 1st is easy and we got it directly from nptel by sir YN srikant Lectures on run time environment.

statement 2nd is also well understood as we know c not allowed automatic GC and we can implement recursion there.

statement 4th is also fine there is quite difficulties to implementation recursion in heap because we know heap allow random allocation and de-allocation and moreover there is a simulation required for a well formed data structure.

Only Statement 3rd i am not getting here can anybody explain this? 

Acc to my point of view activation record is created whenever there will be a entry of a procedure thats why there is a need of recursion.

Correct me...

 

0
0
2 votes
2 votes

This question can be solved by option elimination.

See the 4 th statement, it says both stack and heap are essential to implement recursion. But we know that only stack is enough to implement recursion. Hence 4 th statement is false.

 

Similarly statement 2 says automatic  garbage collection is essential for recursion which is again false as there is no relationship of automatic garbage collection and recursion. 

So statement 4 and 2 are false. 

Hence we are left with only as the correct answer.

 

 

1 comment

wouldn’t option 3 being true would also by extension imply option 4 being true?
0
0
Answer:

Related questions