Bird
Raised Fist0
Javascriptprogramming~20 mins

Call stack behavior in Javascript - 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
What is the output of nested function calls?
Consider the following JavaScript code. What will be printed to the console when start() is called?
Javascript
function first() {
  console.log('first');
}

function second() {
  first();
  console.log('second');
}

function start() {
  second();
  console.log('start');
}

start();
A"first" "second" "start"
B"start" "second" "first"
C"second" "first" "start"
D"first" "start" "second"
Attempts:
2 left
💡 Hint
Think about the order functions are called and when console.log runs inside each.
❓ Predict Output
intermediate
2:00remaining
What happens with recursive calls?
Look at this recursive function. What will be the last number printed before the function stops?
Javascript
function countdown(n) {
  if (n <= 0) {
    console.log('Done');
    return;
  }
  console.log(n);
  countdown(n - 1);
}

countdown(3);
A"3"
B"1"
C"Done"
D"0"
Attempts:
2 left
💡 Hint
Think about when the base case is reached and what it prints.
❓ Predict Output
advanced
2:00remaining
What is the output with asynchronous calls and call stack?
What will be the order of console logs when this code runs?
Javascript
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
A"Start" "Timeout" "End"
B"Timeout" "Start" "End"
C"End" "Start" "Timeout"
D"Start" "End" "Timeout"
Attempts:
2 left
💡 Hint
Remember that setTimeout callback runs after the current call stack is empty.
❓ Predict Output
advanced
2:00remaining
What error occurs with infinite recursion?
What error will this code produce when run?
Javascript
function recurse() {
  recurse();
}

recurse();
AReferenceError: recurse is not defined
BRangeError: Maximum call stack size exceeded
CSyntaxError: Unexpected token
DTypeError: recurse is not a function
Attempts:
2 left
💡 Hint
Think about what happens when a function calls itself endlessly without stopping.
🧠 Conceptual
expert
3:00remaining
How does the call stack handle nested asynchronous functions?
Given the code below, what is the order of console logs printed?
Javascript
async function first() {
  console.log('first start');
  await second();
  console.log('first end');
}

async function second() {
  console.log('second start');
  await Promise.resolve();
  console.log('second end');
}

first();
console.log('script end');
A"first start" "second start" "script end" "second end" "first end"
B"script end" "first start" "second start" "second end" "first end"
C"first start" "second start" "second end" "first end" "script end"
D"first start" "script end" "second start" "second end" "first end"
Attempts:
2 left
💡 Hint
Remember that await pauses the async function and lets the event loop run other code.

Practice

(1/5)
1. What does the call stack do in JavaScript?
easy
A. Keeps track of function calls in the order they are called
B. Stores variables and their values
C. Manages asynchronous events
D. Handles network requests

Solution

  1. Step 1: Understand the role of the call stack

    The call stack tracks which functions are currently running and in what order.
  2. Step 2: Differentiate from other JavaScript components

    Variables are stored in memory, asynchronous events are handled by the event loop, and network requests are managed separately.
  3. Final Answer:

    Keeps track of function calls in the order they are called -> Option A
  4. Quick Check:

    Call stack = function order tracking [OK]
Hint: Call stack = function call order tracker [OK]
Common Mistakes:
  • Confusing call stack with event loop
  • Thinking call stack stores variables
  • Mixing call stack with network handling
2. Which of the following is the correct way to call a function named foo in JavaScript?
easy
A. call foo();
B. function foo();
C. execute foo();
D. foo();

Solution

  1. Step 1: Recall JavaScript function call syntax

    Functions are called by writing their name followed by parentheses, like foo();.
  2. Step 2: Identify incorrect options

    "call foo();" and "execute foo();" are not valid JavaScript syntax. "function foo();" is a function declaration, not a call.
  3. Final Answer:

    foo(); -> Option D
  4. Quick Check:

    Function call = name + () [OK]
Hint: Call functions with name and parentheses () [OK]
Common Mistakes:
  • Using 'call' keyword incorrectly
  • Confusing declaration with call
  • Adding extra keywords like 'execute'
3. What will be the output of the following code?
function first() {
  console.log('first');
  second();
  console.log('first again');
}

function second() {
  console.log('second');
}

first();
medium
A. second\nfirst\nfirst again
B. first\nsecond\nfirst again
C. first\nfirst again\nsecond
D. first again\nsecond\nfirst

Solution

  1. Step 1: Trace the call stack execution order

    Calling first() prints 'first', then calls second() which prints 'second'. After second() finishes, control returns to first() and prints 'first again'.
  2. Step 2: Confirm output sequence

    The output lines are: 'first', 'second', 'first again' in that order.
  3. Final Answer:

    first second first again -> Option B
  4. Quick Check:

    Call stack order = first, second, first again [OK]
Hint: Follow function calls step-by-step [OK]
Common Mistakes:
  • Assuming all logs run before second()
  • Mixing order of console logs
  • Ignoring return to first() after second()
4. Identify the error in the following code related to call stack behavior:
function a() {
  b();
}

function b() {
  a();
}

a();
medium
A. Function b is not defined
B. Syntax error due to missing semicolons
C. Infinite recursion causing stack overflow
D. No error, code runs fine

Solution

  1. Step 1: Analyze function calls and recursion

    Function a() calls b(), and b() calls a() again, creating an endless loop.
  2. Step 2: Understand call stack impact

    This infinite mutual recursion causes the call stack to grow until it overflows, leading to a runtime error.
  3. Final Answer:

    Infinite recursion causing stack overflow -> Option C
  4. Quick Check:

    Mutual recursion without stop = stack overflow [OK]
Hint: Watch for functions calling each other endlessly [OK]
Common Mistakes:
  • Thinking missing semicolons cause error
  • Assuming functions are undefined
  • Believing code runs without error
5. Consider this code:
function factorial(n) {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}

console.log(factorial(3));

How many times is the factorial function added to the call stack during factorial(3) execution?
hard
A. 4 times
B. 6 times
C. 1 time
D. 3 times

Solution

  1. Step 1: Trace recursive calls for factorial(3)

    Calls happen for n=3, n=2, n=1, and n=0. Each call adds a new frame to the call stack.
  2. Step 2: Count total calls added to the stack

    There are 4 calls total before reaching the base case (n=0), so 4 stack frames are added.
  3. Final Answer:

    4 times -> Option A
  4. Quick Check:

    Recursive calls = n+1 stack frames [OK]
Hint: Count calls including base case for recursion [OK]
Common Mistakes:
  • Counting only n times, missing base case call
  • Confusing total calls with return steps
  • Assuming single call for recursion
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