Crack the Event Loop: Advanced JavaScript Interview Challenge Quiz

  1. Question 1: Order of Execution

    Given the following code snippet, in what order will the numbers be logged? Example: setTimeout(() =u003E console.log(1), 0); Promise.resolve().then(() =u003E console.log(2)); console.log(3);

    1. 3, 2, 1
    2. 2, 3, 1
    3. 3, 1, 2
    4. 2, 1, 3
    5. 1, 2, 3
  2. Question 2: Microtasks vs Macrotasks

    Which of the following statements about JavaScript’s event loop is correct? (Example: Promise callbacks run before setTimeout callbacks.)

    1. setTimeout callbacks always run before Promise.then callbacks
    2. Promise.then callbacks are microtasks and run after all macrotasks
    3. Promise.then callbacks are microtasks and run before setTimeout callbacks
    4. setTimeout callbacks are microtasks
    5. Microtasks and macrotasks are executed in random order
  3. Question 3: Immediate Execution in Promises

    What does the following code output and why? Example: new Promise(() =u003E console.log('A'));

    1. 'A' is never printed because promises are lazy
    2. 'A' is printed synchronously
    3. 'A' is printed after the call stack clears
    4. 'A' is printed asynchronously after other microtasks
    5. 'A' is printed twice
  4. Question 4: Async Functions u0026 Await

    Suppose you have: (async () =u003E { console.log('X'); })(); What is the nature of the output?

    1. 'X' is logged synchronously before all other script code
    2. 'X' is logged after all promises resolve
    3. 'X' is logged synchronously like a regular function call
    4. 'X' is logged after a zero-delay setTimeout
    5. 'X' is not logged at all
  5. Question 5: queueMicrotask Usage

    What is the difference between Promise.resolve().then(fn) and queueMicrotask(fn) when scheduling a function in JavaScript?

    1. queueMicrotask is slower than Promise.then
    2. queueMicrotask puts the function in the macrotask queue, Promise.then uses microtask
    3. There is no difference; both always run after all macrotasks
    4. queueMicrotask is not affected by unhandled promise rejections, unlike Promise.then
    5. Promise.then guarantees higher priority over queueMicrotask
  6. Question 6: Synchronous Promise Executor

    In which phase of the event loop does the executor function passed to new Promise get executed? For example: new Promise(exec).

    1. It executes in the microtask phase
    2. It is deferred until the next tick
    3. It executes synchronously during the current call stack
    4. It waits until all previous promises are settled
    5. It is batched and runs as a macroqueue task
  7. Question 7: Multiple Async Mechanisms

    Consider this sequence: setTimeout(()=u003Econsole.log('A'),0); Promise.resolve().then(()=u003Econsole.log('B')); queueMicrotask(()=u003Econsole.log('C')); console.log('D'); What is the logging order?

    1. D, C, B, A
    2. D, B, C, A
    3. D, B, A, C
    4. C, D, B, A
    5. B, C, D, A
  8. Question 8: Error Propagation in Promises

    What happens if you write: Promise.reject('error').then(() =u003E console.log('Y'), () =u003E console.log('Z'));

    1. 'Y' is logged
    2. 'Z' is logged
    3. No output because of rejection
    4. An uncaught error is thrown and nothing logs
    5. 'Y' and then 'Z' are both logged
  9. Question 9: Await and the Call Stack

    If you execute: async function test() { await null; console.log('Hi'); } test(); console.log('Bye'); What is the output order?

    1. 'Hi' before 'Bye'
    2. 'Bye' then 'Hi'
    3. 'Hi' and 'Bye' together
    4. 'Hi' twice and then 'Bye'
    5. 'Bye', then nothing else
  10. Question 10: Typo Trap: Event Loop Terms

    Which of the following is NOT a valid term related to the JavaScript event loop?

    1. Microtask queue
    2. Macrotask queue
    3. Macroqueue
    4. Call stack
    5. Event loop