The call stack is a special area in memory that stores information about active functions or methods. It keeps track of where each function should return after it finishes.
Click to reveal answer
beginner
What happens when a function is called in terms of the call stack?
When a function is called, a new stack frame is created and pushed onto the call stack. This frame holds the function's parameters, local variables, and return address.
Click to reveal answer
intermediate
Explain what a stack frame contains.
A stack frame contains the function's parameters, local variables, and the return address to know where to continue after the function finishes.
Click to reveal answer
beginner
What happens to the call stack when a function returns?
When a function returns, its stack frame is popped off the call stack, and the program continues execution from the return address stored in the previous stack frame.
Click to reveal answer
intermediate
What is a stack overflow and when can it happen?
A stack overflow happens when the call stack grows beyond its limit, usually because of too many nested function calls or infinite recursion, causing the program to crash.
Click to reveal answer
What does the call stack store when a function is called?
AOnly the function's name
BGlobal variables only
CThe entire program's code
DThe function's parameters, local variables, and return address
✗ Incorrect
The call stack stores the function's parameters, local variables, and return address in a stack frame.
What happens to the call stack when a function finishes execution?
AThe current stack frame is popped
BA new stack frame is pushed
CThe stack is cleared completely
DNothing changes
✗ Incorrect
When a function finishes, its stack frame is popped off the call stack.
Which of these can cause a stack overflow?
ADeclaring local variables
BToo many nested function calls
CUsing global variables
DCalling functions once
✗ Incorrect
Too many nested function calls or infinite recursion can cause the call stack to overflow.
Where does the program continue after a function returns?
AAt a random memory location
BAt the start of the program
CAt the return address stored in the previous stack frame
DAt the end of the program
✗ Incorrect
The program continues at the return address stored in the previous stack frame.
What is NOT stored in a stack frame?
AGlobal variables
BFunction parameters
CReturn address
DLocal variables
✗ Incorrect
Global variables are stored in a different memory area, not in the stack frame.
Describe the lifecycle of a function call in terms of the call stack.
Think about what happens when you call a function and when it finishes.
You got /5 concepts.
Explain what causes a stack overflow and how it relates to the call stack.
Imagine calling functions inside functions without stopping.
You got /4 concepts.
Practice
(1/5)
1. What does the call stack do when a function is called in C++?
easy
A. It adds a new frame for the function call on top of the stack.
B. It removes the last function call from the stack.
C. It clears all previous function calls.
D. It pauses the program execution permanently.
Solution
Step 1: Understand function call behavior
When a function is called, the program needs to remember where to return after the function finishes.
Step 2: Role of the call stack
The call stack adds a new frame on top to store this information and local variables.
Final Answer:
It adds a new frame for the function call on top of the stack. -> Option A
Quick Check:
Function call = add frame [OK]
Hint: Call means add frame; return means remove frame [OK]
Common Mistakes:
Thinking the stack removes frames on function call
Believing the stack clears all calls at once
Confusing call stack with program pause
2. Which of the following C++ code snippets correctly shows a function call that affects the call stack?
easy
A. int main() { return 0; } void foo() { foo(); }
B. void foo() { main(); } int main() { return 0; }
C. void foo() {} int main() { foo(); return 0; }
D. int main() { int x = 5; }
Solution
Step 1: Identify valid function call from main
void foo() {} int main() { foo(); return 0; } calls foo() from main, which adds a frame to the call stack.
Step 2: Check other options for correctness
void foo() { main(); } int main() { return 0; } calls main() from foo(), which is invalid as main should not be called explicitly. int main() { return 0; } void foo() { foo(); } has foo calling itself but foo is not called from main, so no stack change from main. int main() { int x = 5; } has no function calls.
Final Answer:
void foo() {} int main() { foo(); return 0; } -> Option C
Quick Check:
Valid call from main = void foo() {} int main() { foo(); return 0; } [OK]
Hint: Look for main calling another function directly [OK]
Common Mistakes:
Thinking main can be called like a normal function
Ignoring function call order
Confusing declaration with call
3. What is the output of this C++ program?
#include <iostream>
void print(int n) {
if (n == 0) return;
print(n - 1);
std::cout << n << " ";
}
int main() {
print(3);
return 0;
}
medium
A. 3 2 1
B. 1 2 3
C. 0 1 2 3
D. 3 2 1 0
Solution
Step 1: Trace recursive calls
print(3) calls print(2), which calls print(1), which calls print(0). At print(0), it returns immediately.
Step 2: Print after returning from recursion
After print(0) returns, print(1) prints '1 ', then print(2) prints '2 ', then print(3) prints '3 '.
Final Answer:
1 2 3 -> Option B
Quick Check:
Recursion prints after calls = 1 2 3 [OK]
Hint: Print happens after recursion call returns [OK]
Common Mistakes:
Printing before recursive call instead of after
Including 0 in output incorrectly
Confusing order of prints in recursion
4. The following code causes a runtime error. What is the main cause?