0
0
C++programming~10 mins

Call stack behavior in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the function foo.

C++
#include <iostream>

void foo() {
    std::cout << "Hello from foo!" << std::endl;
}

int main() {
    [1];
    return 0;
}
Drag options to blanks, or click blank then click option'
Afoo()
Bfoo
Ccall foo()
Dfoo();
Attempts:
3 left
💡 Hint
Common Mistakes
Writing only the function name without parentheses.
Adding extra words like 'call' before the function name.
2fill in blank
medium

Complete the code to make bar call foo.

C++
#include <iostream>

void foo() {
    std::cout << "Inside foo" << std::endl;
}

void bar() {
    [1];
}

int main() {
    bar();
    return 0;
}
Drag options to blanks, or click blank then click option'
Afoo();
Bfoo
Ccall foo()
Dfoo()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses when calling the function.
Adding extra words like 'call' before the function name.
3fill in blank
hard

Fix the error in the recursive function countdown to avoid infinite recursion.

C++
#include <iostream>

void countdown(int n) {
    if (n == 0) {
        std::cout << "Done!" << std::endl;
        return;
    }
    std::cout << n << std::endl;
    countdown([1]);
}

int main() {
    countdown(3);
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
Bn + 1
Cn - 1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Calling with the same number causing infinite recursion.
Increasing the number instead of decreasing.
4fill in blank
hard

Fill both blanks to create a dictionary-like map of numbers to their squares using a loop.

C++
#include <iostream>
#include <map>

int main() {
    std::map<int, int> squares;
    for (int [1] = 1; [2] <= 5; ++i) {
        squares[i] = i * i;
    }
    for (const auto& [key, value] : squares) {
        std::cout << key << ": " << value << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition.
Using variables not declared in the loop.
5fill in blank
hard

Fill all three blanks to create a recursive function that sums numbers from n down to 1.

C++
#include <iostream>

int sum_to_n(int [1]) {
    if ([2] == 0) {
        return 0;
    }
    return [3] + sum_to_n([2] - 1);
}

int main() {
    std::cout << sum_to_n(5) << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
An
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Forgetting to decrease the number in the recursive call.