JavaScript Event Loop and Asynchronous Programming Quiz
Want to learn more than this quiz offers you? Have a look at my Frontend web
development courses.
Create an account and save your quiz results
Login and save your results
OR
Question 1/10
Which of the following statements about `setImmediate` in Node.js is true?
Select your answer
Question 2/10
What is the primary role of the JavaScript event loop in the browser?
Select your answer
Question 3/10
What will the following code output to the console? Assume a Node.js environment.
console.log('First');
setTimeout(() => {
console.log('Timeout');
}, 0);
setImmediate(() => {
console.log('Immediate');
});
process.nextTick(() => {
console.log('Next Tick');
});
console.log('Last');
Select your answer
Question 4/10
How does JavaScript handle I/O operations like reading files with respect to the event loop?
Select your answer
Question 5/10
Which of the following best describes event delegation in JavaScript?
Select your answer
Question 6/10
In the JavaScript event loop model, what is the main purpose of the microtask queue?
Select your answer
Question 7/10
What impact does a long-running synchronous task have on the JavaScript event loop?
Select your answer
Question 8/10
What does the call stack do when a function is called in JavaScript?
Select your answer
Question 9/10
What will be the output of the following code snippet when logged to the console?
async function fetchData() {
return 'Data fetched';
}
const data = fetchData();
data.then(console.log);
console.log(data instanceof Promise);
Select your answer
Question 10/10
Consider the following JavaScript code snippet. What will be logged to the console?
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
Promise.resolve().then(() => {
console.log('Promise');
});
console.log('End');
Select your answer
Your Results
You did not answer any questions correctly.
Your Answers
Question 1/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which of the following statements about `setImmediate` in Node.js is true?
Available answers
In Node.js, `setImmediate` is designed to execute a specified function at the end of the current event loop cycle, after I/O events, but before any `setTimeout` or event queue handlers unless specifically delayed. It is not equivalent to `setTimeout(function, 0)`, which schedules the callback within the event loop but after completing all other tasks already queued.
Question 2/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is the primary role of the JavaScript event loop in the browser?
Available answers
The event loop in JavaScript is responsible for handling asynchronous operations. It continuously checks if the call stack is empty and if there are any callbacks waiting in the queue to be executed. When the call stack is empty, it dequeues a callback and places it in the call stack to execute it. This is essential for non-blocking code execution.
Question 3/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will the following code output to the console? Assume a Node.js environment.
console.log('First');
setTimeout(() => {
console.log('Timeout');
}, 0);
setImmediate(() => {
console.log('Immediate');
});
process.nextTick(() => {
console.log('Next Tick');
});
console.log('Last');
Available answers
The correct order is 'First', 'Last', 'Next Tick', 'Immediate', 'Timeout'. In Node.js, `process.nextTick()` callbacks are processed after the current operation and before any I/O tasks or timers, which explains why 'Next Tick' runs after 'Last'. Then, `setImmediate` can be invoked before `setTimeout` even if both are scheduled at zero delay.
Question 4/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
How does JavaScript handle I/O operations like reading files with respect to the event loop?
Available answers
JavaScript, especially in environments like Node.js, handles I/O operations asynchronously. When an I/O operation is initiated, a callback is registered, and the call stack continues to execute other code without blocking. Once the I/O operation completes, the callback is placed in the event queue to be executed by the event loop.
Question 5/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which of the following best describes event delegation in JavaScript?
Available answers
Event delegation involves adding a single event listener to a common ancestor instead of individual elements. By taking advantage of event propagation (bubbling), the event listener can handle events triggered on child elements. This improves performance and simplifies code maintenance.
Question 6/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
In the JavaScript event loop model, what is the main purpose of the microtask queue?
Available answers
The microtask queue is used to execute tasks immediately after the current operation ends, but before any subsequent events in the event queue. Promises and `process.nextTick()` in Node.js typically use this queue. This prioritization ensures that microtasks are handled as soon as possible, often used for promise resolution callbacks.
Question 7/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
What impact does a long-running synchronous task have on the JavaScript event loop?
Available answers
A long-running synchronous task blocks the call stack, which means no other asynchronous callbacks can be invoked until it finishes. This can lead to poor performance and a non-responsive application since the event loop cannot process any other events until the task is completed.
Question 8/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
What does the call stack do when a function is called in JavaScript?
Available answers
When a function is called, it is pushed onto the call stack. The JavaScript engine executes the function, and once it completes, it is popped off the stack. This process repeats for nested or recursive functions, following the LIFO (Last In, First Out) principle.
Question 9/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be the output of the following code snippet when logged to the console?
async function fetchData() {
return 'Data fetched';
}
const data = fetchData();
data.then(console.log);
console.log(data instanceof Promise);
Available answers
The `fetchData` function is declared as `async`, which means it always returns a promise. Thus, `data` is an instance of `Promise`, logging `true`. The `Promise` is resolved after the synchronous code, logging the resolution, which calls `console.log` with 'Data fetched'. The actual logging of the promise takes place when the `.then(console.log)` executes the callback in the microtask queue.
Question 10/10
😊 Your
answer was correct
🙁 Your
answer was incorrect
Consider the following JavaScript code snippet. What will be logged to the console?
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
Promise.resolve().then(() => {
console.log('Promise');
});
console.log('End');
Available answers
The correct order is 'Start', 'End', 'Promise', 'Timeout'. The reason is that the synchronous code runs first ('Start' and 'End' are logged). Then, the microtask (promise) queue is processed before the macrotask queue (setTimeout), so 'Promise' is logged before 'Timeout'.