0
0
Cprogramming~20 mins

Call stack behavior - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Call Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this recursive function?

Consider the following C code that uses recursion. What will it print when run?

C
#include <stdio.h>

void printNumbers(int n) {
    if (n == 0) return;
    printNumbers(n - 1);
    printf("%d ", n);
}

int main() {
    printNumbers(3);
    return 0;
}
A3 2 1
B1 2 3
C0 1 2 3
D3 2 1 0
Attempts:
2 left
💡 Hint

Think about when the printf statement runs during the recursion.

Predict Output
intermediate
2:00remaining
What is the output of nested function calls?

Analyze the output of this C program with nested function calls.

C
#include <stdio.h>

void f1() {
    printf("A");
}

void f2() {
    printf("B");
    f1();
    printf("C");
}

int main() {
    f2();
    return 0;
}
ACAB
BABC
CBCA
DBAC
Attempts:
2 left
💡 Hint

Follow the order of function calls and prints carefully.

🔧 Debug
advanced
2:00remaining
What error occurs in this recursive function?

Examine this recursive function. What error will it cause when run?

C
#include <stdio.h>

void recurse(int n) {
    printf("%d ", n);
    recurse(n + 1);
}

int main() {
    recurse(1);
    return 0;
}
AStack overflow (infinite recursion)
BSegmentation fault due to null pointer
CCompilation error: missing return type
DNo error, prints numbers from 1 to 10
Attempts:
2 left
💡 Hint

Consider what happens when the function never stops calling itself.

📝 Syntax
advanced
2:00remaining
Which option causes a stack corruption due to wrong function pointer usage?

Identify which code snippet will cause stack corruption because of incorrect function pointer usage.

Avoid (*fp)() = (void (*)())1234; fp();
Bvoid (*fp)() = &main; fp();
Cvoid (*fp)() = NULL; if(fp) fp();
Dvoid (*fp)() = &printf; fp();
Attempts:
2 left
💡 Hint

Think about what happens when you call a function pointer set to an invalid address.

🚀 Application
expert
2:00remaining
How many stack frames are active at the deepest point?

Given this recursive function, how many stack frames exist at the deepest recursion call?

C
#include <stdio.h>

void countDown(int n) {
    if (n == 0) return;
    countDown(n - 1);
    printf("%d ", n);
}

int main() {
    countDown(5);
    return 0;
}
A5
B4
C6
D1
Attempts:
2 left
💡 Hint

Remember the base case call also creates a stack frame.