Bird
Raised Fist0
C++programming~20 mins

Call stack behavior in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Call Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested function calls with stack frames
What is the output of the following C++ program that uses nested function calls?
void funcC() {
    std::cout << "C ";
}

void funcB() {
    std::cout << "B ";
    funcC();
}

void funcA() {
    std::cout << "A ";
    funcB();
}

int main() {
    funcA();
    return 0;
}
C++
void funcC() {
    std::cout << "C ";
}

void funcB() {
    std::cout << "B ";
    funcC();
}

void funcA() {
    std::cout << "A ";
    funcB();
}

int main() {
    funcA();
    return 0;
}
AA B C
BC B A
CA C B
DB A C
Attempts:
2 left
💡 Hint
Think about the order in which functions are called and how the call stack works.
❓ Predict Output
intermediate
2:00remaining
Value of variable after recursive calls
What is the value of variable result after running this C++ recursive function?
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int result = factorial(4);
    std::cout << result;
    return 0;
}
C++
int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int result = factorial(4);
    std::cout << result;
    return 0;
}
A16
B24
C10
D120
Attempts:
2 left
💡 Hint
Recall how factorial is calculated recursively.
❓ Predict Output
advanced
2:00remaining
Call stack depth with mutual recursion
Consider the following C++ code with mutual recursion. What is the output?
void funcA(int n);
void funcB(int n);

void funcA(int n) {
    if (n == 0) return;
    std::cout << "A";
    funcB(n - 1);
}

void funcB(int n) {
    if (n == 0) return;
    std::cout << "B";
    funcA(n / 2);
}

int main() {
    funcA(4);
    return 0;
}
C++
void funcA(int n);
void funcB(int n);

void funcA(int n) {
    if (n == 0) return;
    std::cout << "A";
    funcB(n - 1);
}

void funcB(int n) {
    if (n == 0) return;
    std::cout << "B";
    funcA(n / 2);
}

int main() {
    funcA(4);
    return 0;
}
AABABB
BABABA
CABAB
DABA
Attempts:
2 left
💡 Hint
Trace the calls carefully, noting how n changes.
❓ Predict Output
advanced
2:00remaining
Stack unwinding with exception handling
What is the output of this C++ program demonstrating stack unwinding with exceptions?
void func3() {
    std::cout << "func3 start\n";
    throw std::runtime_error("error");
    std::cout << "func3 end\n";
}

void func2() {
    std::cout << "func2 start\n";
    func3();
    std::cout << "func2 end\n";
}

void func1() {
    std::cout << "func1 start\n";
    try {
        func2();
    } catch (...) {
        std::cout << "Exception caught in func1\n";
    }
    std::cout << "func1 end\n";
}

int main() {
    func1();
    return 0;
}
C++
void func3() {
    std::cout << "func3 start\n";
    throw std::runtime_error("error");
    std::cout << "func3 end\n";
}

void func2() {
    std::cout << "func2 start\n";
    func3();
    std::cout << "func2 end\n";
}

void func1() {
    std::cout << "func1 start\n";
    try {
        func2();
    } catch (...) {
        std::cout << "Exception caught in func1\n";
    }
    std::cout << "func1 end\n";
}

int main() {
    func1();
    return 0;
}
A
func1 start
func2 start
func3 start
Exception caught in func1
func1 end
B
func1 start
func2 start
func3 start
func3 end
func2 end
func1 end
C
func1 start
func2 start
Exception caught in func1
func1 end
D
func1 start
func2 start
func3 start
func2 end
func1 end
Attempts:
2 left
💡 Hint
Remember that code after throw is not executed and stack unwinds to catch block.
❓ Predict Output
expert
2:00remaining
Call stack size after deep recursion
What is the output of this C++ program that counts recursive calls?
int count = 0;

void recurse(int n) {
    if (n == 0) return;
    count++;
    recurse(n - 1);
}

int main() {
    recurse(1000);
    std::cout << count;
    return 0;
}
C++
int count = 0;

void recurse(int n) {
    if (n == 0) return;
    count++;
    recurse(n - 1);
}

int main() {
    recurse(1000);
    std::cout << count;
    return 0;
}
A1001
B999
C1000
D0
Attempts:
2 left
💡 Hint
Count increments before each recursive call until n reaches zero.

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

  1. Step 1: Understand function call behavior

    When a function is called, the program needs to remember where to return after the function finishes.
  2. Step 2: Role of the call stack

    The call stack adds a new frame on top to store this information and local variables.
  3. Final Answer:

    It adds a new frame for the function call on top of the stack. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    void foo() {} int main() { foo(); return 0; } -> Option C
  4. 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

  1. Step 1: Trace recursive calls

    print(3) calls print(2), which calls print(1), which calls print(0). At print(0), it returns immediately.
  2. 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 '.
  3. Final Answer:

    1 2 3 -> Option B
  4. 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?
void recurse(int n) {
  if (n > 0) {
    recurse(n + 1);
  }
}

int main() {
  recurse(1);
  return 0;
}
medium
A. Stack overflow due to infinite recursion increasing n.
B. Syntax error because of missing return statement.
C. No error; program runs and exits normally.
D. Variable n is not initialized.

Solution

  1. Step 1: Analyze recursion condition

    The function calls recurse(n + 1) if n > 0, so n increases each call without stopping.
  2. Step 2: Understand call stack effect

    Because n never reaches 0 or less, recursion never stops, causing stack frames to pile up until overflow.
  3. Final Answer:

    Stack overflow due to infinite recursion increasing n. -> Option A
  4. Quick Check:

    Infinite recursion = stack overflow [OK]
Hint: Check if recursion moves toward base case [OK]
Common Mistakes:
  • Assuming recursion stops without base case
  • Confusing syntax error with runtime error
  • Ignoring infinite growth of parameter
5. Given this code, what will be the final value of result?
int factorial(int n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

int main() {
  int result = factorial(4);
  return 0;
}
hard
A. 1
B. 10
C. 4
D. 24

Solution

  1. Step 1: Understand factorial recursion

    factorial(4) calls factorial(3), factorial(3) calls factorial(2), factorial(2) calls factorial(1), which returns 1.
  2. Step 2: Calculate return values step-by-step

    factorial(2) = 2 * 1 = 2; factorial(3) = 3 * 2 = 6; factorial(4) = 4 * 6 = 24.
  3. Final Answer:

    24 -> Option D
  4. Quick Check:

    4! = 24 [OK]
Hint: Multiply down from n to 1 recursively [OK]
Common Mistakes:
  • Stopping recursion too early
  • Adding instead of multiplying
  • Confusing base case return value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard