Challenge - 5 Problems
Call Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Think about the order in which functions are called and how the call stack works.
✗ Incorrect
The main function calls funcA, which prints 'A ' and calls funcB. funcB prints 'B ' and calls funcC, which prints 'C '. The output is 'A B C '.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Recall how factorial is calculated recursively.
✗ Incorrect
factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1) = 4 * 3 * 2 * 1 = 24.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Trace the calls carefully, noting how n changes.
✗ Incorrect
funcA(4) prints 'A' then calls funcB(3). funcB(3) prints 'B' then calls funcA(1). funcA(1) prints 'A' then calls funcB(0). funcB(0) returns. So output is 'ABA'.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Remember that code after throw is not executed and stack unwinds to catch block.
✗ Incorrect
func3 prints 'func3 start' then throws an exception, so 'func3 end' is skipped. func2 does not print 'func2 end' because func3 threw. The exception is caught in func1, which prints the catch message and then 'func1 end'.
❓ Predict Output
expert2: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; }
Attempts:
2 left
💡 Hint
Count increments before each recursive call until n reaches zero.
✗ Incorrect
The function increments count for each call except when n is zero. Since recurse(1000) calls recurse(999) ... recurse(1), count increments 1000 times.