0
0
C++programming~20 mins

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

Choose your learning style9 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.