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. A. 1 and 2 only B. 2 and 3 only C. 3 and 4 only D. 1 and 3 only
GATE 2014 · Compiler Design · Runtime Environment · medium
Answer: D. 1 and 3 only — static allocation prevents recursion (stmt 1 true) and dynamic activation records are essential (stmt 3 true).
- Check Statement 1 and 3: Statement 1: Static allocation assigns a single fixed address to each local variable of a function. When a function calls itself recursively, all active calls would share the same storage, corrupting each other's data. Hence recursion is impossible under pure static allocation. TRUE. Statement 3: Dynamic allocation of activation records (pushing/popping stack frames) allows each invocation to have private storage. This is exactly what modern compilers do using the runtime stack. TRUE.
- Check Statement 2 and 4: Statement 2: Automatic garbage collection manages heap objects. Recursion is implemented via the call stack, not the heap. Languages like C implement recursion without any GC. FALSE. Statement 4: Only the stack is essential for recursion; the heap is not required. Purely stack-based runtimes (e.g., C, assembly) support recursion without a heap. FALSE.