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);
- 3, 2, 1
- 2, 3, 1
- 3, 1, 2
- 2, 1, 3
- 1, 2, 3
Question 2: Microtasks vs Macrotasks
Which of the following statements about JavaScript’s event loop is correct? (Example: Promise callbacks run before setTimeout callbacks.)
- setTimeout callbacks always run before Promise.then callbacks
- Promise.then callbacks are microtasks and run after all macrotasks
- Promise.then callbacks are microtasks and run before setTimeout callbacks
- setTimeout callbacks are microtasks
- Microtasks and macrotasks are executed in random order
Question 3: Immediate Execution in Promises
What does the following code output and why? Example: new Promise(() =u003E console.log('A'));
- 'A' is never printed because promises are lazy
- 'A' is printed synchronously
- 'A' is printed after the call stack clears
- 'A' is printed asynchronously after other microtasks
- 'A' is printed twice
Question 4: Async Functions u0026 Await
Suppose you have: (async () =u003E { console.log('X'); })(); What is the nature of the output?
- 'X' is logged synchronously before all other script code
- 'X' is logged after all promises resolve
- 'X' is logged synchronously like a regular function call
- 'X' is logged after a zero-delay setTimeout
- 'X' is not logged at all
Question 5: queueMicrotask Usage
What is the difference between Promise.resolve().then(fn) and queueMicrotask(fn) when scheduling a function in JavaScript?
- queueMicrotask is slower than Promise.then
- queueMicrotask puts the function in the macrotask queue, Promise.then uses microtask
- There is no difference; both always run after all macrotasks
- queueMicrotask is not affected by unhandled promise rejections, unlike Promise.then
- Promise.then guarantees higher priority over queueMicrotask
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).
- It executes in the microtask phase
- It is deferred until the next tick
- It executes synchronously during the current call stack
- It waits until all previous promises are settled
- It is batched and runs as a macroqueue task
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?
- D, C, B, A
- D, B, C, A
- D, B, A, C
- C, D, B, A
- B, C, D, A
Question 8: Error Propagation in Promises
What happens if you write: Promise.reject('error').then(() =u003E console.log('Y'), () =u003E console.log('Z'));
- 'Y' is logged
- 'Z' is logged
- No output because of rejection
- An uncaught error is thrown and nothing logs
- 'Y' and then 'Z' are both logged
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?
- 'Hi' before 'Bye'
- 'Bye' then 'Hi'
- 'Hi' and 'Bye' together
- 'Hi' twice and then 'Bye'
- 'Bye', then nothing else
Question 10: Typo Trap: Event Loop Terms
Which of the following is NOT a valid term related to the JavaScript event loop?
- Microtask queue
- Macrotask queue
- Macroqueue
- Call stack
- Event loop