Handling of procedure calls and returns(MCQs)

Basics of Procedure Calls and Returns

What is the primary purpose of a procedure call in a programming language?

A) To execute a block of code defined elsewhere
B) To allocate memory
C) To initialize variables
D) To perform input/output operations
Answer: A) To execute a block of code defined elsewhere
What is typically pushed onto the stack during a procedure call?

A) Return address and local variables
B) Global variables
C) System registers
D) Heap memory pointers
Answer: A) Return address and local variables
Which of the following is a common register used to store the return address in a procedure call?

A) Program Counter (PC)
B) Stack Pointer (SP)
C) Base Pointer (BP)
D) Instruction Register (IR)
Answer: A) Program Counter (PC)
What does the Base Pointer (BP) register typically store in the context of procedure calls?

A) The address of the current stack frame
B) The return address
C) The instruction being executed
D) The address of global variables
Answer: A) The address of the current stack frame
What happens to the stack frame after a procedure returns?

A) It is popped off the stack
B) It remains on the stack indefinitely
C) It is moved to a global memory area
D) It is archived in the heap
Answer: A) It is popped off the stack

Parameter Passing

Which of the following is a method of parameter passing in procedure calls?

A) Pass-by-value
B) Pass-by-reference
C) Pass-by-name
D) All of the above
Answer: D) All of the above
What is the main advantage of pass-by-reference over pass-by-value?

A) It allows the procedure to modify the original argument
B) It uses less memory
C) It is faster for primitive data types
D) It requires less stack space
Answer: A) It allows the procedure to modify the original argument
In pass-by-value, what does the callee receive?

A) A copy of the actual parameter
B) A reference to the actual parameter
C) The address of the actual parameter
D) The original parameter itself
Answer: A) A copy of the actual parameter
In pass-by-reference, what does the callee receive?

A) The address of the actual parameter
B) A copy of the actual parameter
C) A new value
D) A reference to a local variable
Answer: A) The address of the actual parameter
What is “parameter passing convention”?

A) The rules defining how arguments are passed to procedures
B) The method of variable declaration
C) The technique for memory allocation
D) The strategy for error handling
Answer: A) The rules defining how arguments are passed to procedures

Stack Frames

What is the purpose of a stack frame in procedure calls?

A) To store local variables, return addresses, and saved registers
B) To manage heap memory
C) To handle input/output operations
D) To execute system calls
Answer: A) To store local variables, return addresses, and saved registers
What typically happens to the stack frame when a procedure is called?

A) A new stack frame is pushed onto the stack
B) The current stack frame is popped
C) The stack frame is copied to the heap
D) The stack frame is saved in a global area
Answer: A) A new stack frame is pushed onto the stack
What does the term “stack unwinding” refer to?

A) The process of cleaning up the stack after a procedure returns
B) The process of pushing new stack frames
C) The process of allocating memory on the stack
D) The process of handling function calls
Answer: A) The process of cleaning up the stack after a procedure returns
What does the term “activation record” refer to?

A) The data structure that represents a stack frame for a procedure call
B) The address of the next instruction to execute
C) The process of passing parameters
D) The method of generating intermediate code
Answer: A) The data structure that represents a stack frame for a procedure call
In what scenario might the stack frame be used in a recursive function?

A) Each recursive call generates a new stack frame
B) Recursive functions share a single stack frame
C) Stack frames are not used in recursion
D) Stack frames are allocated in the heap
Answer: A) Each recursive call generates a new stack frame

Procedure Call Mechanism

What does the term “calling convention” refer to?

A) The rules and methods used to make procedure calls and returns
B) The method of managing global variables
C) The strategy for error handling
D) The technique for memory allocation
Answer: A) The rules and methods used to make procedure calls and returns
Which register typically holds the return value of a procedure in many calling conventions?

A) The Accumulator (AX) register
B) The Base Pointer (BP) register
C) The Stack Pointer (SP) register
D) The Program Counter (PC) register
Answer: A) The Accumulator (AX) register
What is the role of the “return address” in a procedure call?

A) It indicates where to continue execution after the procedure returns
B) It stores the value of local variables
C) It manages memory allocation
D) It handles function calls
Answer: A) It indicates where to continue execution after the procedure returns
How is the return address typically stored during a procedure call?

A) It is pushed onto the stack
B) It is stored in a register
C) It is saved in a global variable
D) It is written to the heap
Answer: A) It is pushed onto the stack
What does the term “procedure prologue” refer to?

A) The sequence of instructions that sets up the stack frame at the start of a procedure
B) The sequence of instructions that cleans up the stack frame before returning
C) The instructions that handle parameter passing
D) The instructions for calling other procedures
Answer: A) The sequence of instructions that sets up the stack frame at the start of a procedure

Procedure Epilogue

What does the term “procedure epilogue” refer to?

A) The sequence of instructions that cleans up the stack frame and returns control to the caller
B) The setup of the stack frame for a procedure
C) The instructions for handling recursive calls
D) The management of heap memory
Answer: A) The sequence of instructions that cleans up the stack frame and returns control to the caller
What is typically restored by the procedure epilogue?

A) Saved registers and the Base Pointer (BP)
B) The return address and global variables
C) Memory allocated on the heap
D) The Stack Pointer (SP) and local variables
Answer: A) Saved registers and the Base Pointer (BP)
What is the purpose of the “return instruction” in a procedure epilogue?

A) To transfer control back to the calling procedure
B) To allocate memory for local variables
C) To initialize the stack frame
D) To handle function calls
Answer: A) To transfer control back to the calling procedure
How is the stack pointer typically adjusted during the procedure epilogue?

A) It is restored to its value before the procedure was called
B) It is incremented to allocate more space
C) It is decremented to deallocate memory
D) It is set to zero
Answer: A) It is restored to its value before the procedure was called
What does “register saving” involve in the context of procedure calls?

A) Saving the state of registers before a procedure call and restoring it afterward
B) Allocating additional registers
C) Managing memory allocation
D) Generating intermediate code
Answer: A) Saving the state of registers before a procedure call and restoring it afterward

Calling Conventions

Which of the following is a common calling convention in x86 architecture?

A) cdecl
B) pascal
C) stdcall
D) All of the above
Answer: D) All of the above
In the “cdecl” calling convention, who is responsible for cleaning up the stack after a procedure call?

A) The caller
B) The callee
C) The operating system
D) The compiler
Answer: A) The caller
In the “stdcall” calling convention, who is responsible for cleaning up the stack after a procedure call?

A) The callee
B) The caller
C) The operating system
D) The compiler
Answer: A) The callee
What is a notable feature of the “fastcall” calling convention?

A) It passes the first few arguments in registers rather than on the stack
B) It always passes arguments on the stack
C) It uses a fixed-size stack frame
D) It does not support recursion
Answer: A) It passes the first few arguments in registers rather than on the stack
What does “cdecl” stand for?

A) C Declaration
B) C Declaration Call
C) C Default
D) C Default Calling Convention
Answer: A) C Declaration

Advanced Topics

What is a “tail call optimization”?

A) An optimization that reuses the current function’s stack frame for the called function if it is the last operation
B) An optimization that doubles the stack size for the called function
C) An optimization that delays function calls
D) An optimization that removes the last function call
Answer: A) An optimization that reuses the current function’s stack frame for the called function if it is the last operation
In the context of procedure calls, what does “inlining” refer to?

A) Replacing a procedure call with the procedure’s code to avoid the overhead of a call
B) Increasing the size of the stack frame
C) Managing heap memory
D) Handling function calls sequentially
Answer: A) Replacing a procedure call with the procedure’s code to avoid the overhead of a call
What is “reentrant code”?

A) Code that can be safely interrupted and resumed without affecting correctness
B) Code that executes multiple times simultaneously
C) Code that performs recursive calls
D) Code that manages memory allocation
Answer: A) Code that can be safely interrupted and resumed without affecting correctness
What is the impact of “context switching” on procedure calls?

A) It involves saving and restoring the state of the CPU, which can affect the execution of procedure calls
B) It simplifies stack management
C) It eliminates the need for stack frames
D) It optimizes memory allocation
Answer: A) It involves saving and restoring the state of the CPU, which can affect the execution of procedure calls
What is “dynamic linking”?

A) Linking procedure calls to their implementations at runtime rather than compile-time
B) Linking procedures at compile-time
C) Allocating memory for procedures
D) Managing function calls in a static environment
Answer: A) Linking procedure calls to their implementations at runtime rather than compile-time

Procedure Management

What does “nested procedure call” refer to?

A) A procedure calling another procedure which in turn calls yet another procedure
B) A procedure calling itself
C) A procedure that does not return any value
D) A procedure that performs no operations
Answer: A) A procedure calling another procedure which in turn calls yet another procedure
How does “reentrant code” affect the use of global variables in procedures?

A) Reentrant code should avoid using global variables to prevent data corruption
B) Reentrant code allows global variables to be used freely
C) Reentrant code eliminates the need for global variables
D) Reentrant code only uses global variables for initialization
Answer: A) Reentrant code should avoid using global variables to prevent data corruption
What is “stack overflow”?

A) A condition where the stack grows beyond its allocated space, leading to program errors
B) A condition where the stack is underutilized
C) A condition where the stack frame is not deallocated
D) A condition where stack frames are reused
Answer: A) A condition where the stack grows beyond its allocated space, leading to program errors
What is the “frame pointer” used for in procedure calls?

A) To keep track of the start of the current stack frame
B) To store the return address
C) To manage global variables
D) To perform memory allocation
Answer: A) To keep track of the start of the current stack frame
What is the typical role of the “return value” in a procedure call?

A) To provide a result or output from the procedure to the caller
B) To allocate memory
C) To manage stack frames
D) To handle function calls
Answer: A) To provide a result or output from the procedure to the caller

Optimization and Advanced Concepts

What is “call graph”?

A) A representation of the relationships between different procedures in a program
B) A visualization of stack frames
C) A list of memory allocations
D) A diagram of function calls
Answer: A) A representation of the relationships between different procedures in a program
What does “stack-based memory management” refer to?

A) Using the stack to manage local variables and function calls
B) Managing memory on the heap
C) Using static memory allocation
D) Allocating memory for global variables
Answer: A) Using the stack to manage local variables and function calls
What is “tail recursion”?

A) A recursive call that is the last operation in a function, allowing optimization to reuse the stack frame
B) A recursive call that occurs in the middle of a function
C) A non-recursive function call
D) A function that does not return a value
Answer: A) A recursive call that is the last operation in a function, allowing optimization to reuse the stack frame
What does “recursive procedure call” imply?

A) A procedure calling itself
B) A procedure calling another procedure
C) A procedure that performs no operations
D) A procedure that handles memory allocation
Answer: A) A procedure calling itself
What is the role of “procedure linkage” in code generation?

A) To establish the connections between different procedures and ensure correct calling and returning
B) To manage memory allocation
C) To handle stack frames
D) To perform optimization
Answer: A) To establish the connections between different procedures and ensure correct calling and returning

Stack Management and Miscellaneous

What is the primary function of “frame pointer” in managing nested procedures?

A) To reference the base of the current stack frame and help manage local variables and parameters
B) To store the return address
C) To allocate memory on the heap
D) To handle input/output operations
Answer: A) To reference the base of the current stack frame and help manage local variables and parameters
What is the purpose of “stack unwinding” in exception handling?

A) To clean up stack frames and handle exceptions when an error occurs
B) To allocate memory for local variables
C) To manage function calls
D) To optimize code execution
Answer: A) To clean up stack frames and handle exceptions when an error occurs
What does “dynamic stack allocation” refer to?

A) Allocating stack space dynamically at runtime
B) Allocating stack space statically at compile-time
C) Managing heap memory
D) Handling global variables
Answer: A) Allocating stack space dynamically at runtime
What is “procedure linkage table” (PLT) used for in dynamic linking?

A) To manage and resolve addresses of procedures in shared libraries at runtime
B) To handle memory allocation
C) To manage global variables
D) To perform stack management
Answer: A) To manage and resolve addresses of procedures in shared libraries at runtime
What is the significance of “return address stack” in modern processors?

A) It helps manage the return addresses for nested procedure calls and returns efficiently
B) It stores global variables
C) It allocates heap memory
D) It handles input/output operations
Answer: A) It helps manage the return addresses for nested procedure calls and returns efficiently