Sample questions
Question 1Easy
What is libuv in Node.js?
- The JavaScript engine that parses and compiles Node code
- A package manager bundled with Node.js
- A C library providing the event loop and async I/O
- A web framework for routing HTTP requests
Answer: C. A C library providing the event loop and async I/O
Explanation
V8 executes JavaScript; libuv provides the event loop, the thread pool and cross-platform asynchronous I/O.
Step-by-step solution
- Node.js uses V8 to run JavaScript and libuv for I/O.
- Libuv is a C library that provides the event loop, async I/O and the thread pool.
- So libuv is the C library for the event loop and async I/O, option C.
Why the other options are wrong
- A. That describes V8, the JavaScript engine.
- B. Npm is the package manager bundled with Node.js, not libuv.
- D. A web framework such as Express routes HTTP requests; libuv is not a framework.
Tip: V8 runs JavaScript, libuv runs the event loop and I/O.
Question 2Easy
What is the output of this script?
async function f() {
console.log('in f');
await null;
console.log('after await');
}
f();
console.log('outside');
- in f outside after await
- in f after await outside
- outside in f after await
- after await in f outside
Answer: A. in f outside after await
Explanation
An async function runs synchronously up to its first await. The rest of it resumes as a microtask after the current synchronous code ends.
Step-by-step solution
- f() starts and prints 'in f'.
- Await null pauses f and schedules the rest of f as a microtask.
- f() returns, so 'outside' prints next in the synchronous code.
- The microtask then prints 'after await'. Output: in f outside after await, option A.
Why the other options are wrong
- B. The code after await waits until the synchronous code finishes, so it prints after outside.
- C. In f runs synchronously before f returns, so it prints before outside.
- D. In f prints first, because it runs before the await.
Tip: Code after await always waits for the rest of the synchronous code.
Question 3Easy
A route handler runs a synchronous loop that takes 5 seconds. What happens to other incoming requests during that time?
- They are handled on another JavaScript thread
- They wait, because the event loop is blocked
- Node starts a new process for each of them
- They are rejected with HTTP 503 at once
Answer: B. They wait, because the event loop is blocked
Explanation
JavaScript in a Node process runs on a single main thread. While it is busy, no other callbacks, including new requests, can run.
Step-by-step solution
- JavaScript runs on one main thread only.
- Synchronous code blocks the event loop.
- New requests cannot run while the loop is active.
- Requests wait until the current task finishes.
- So other incoming requests wait, because the event loop is blocked.
Why the other options are wrong
- A. Node runs JavaScript on one main thread, so a loop blocks the event loop, not a separate thread.
- C. Node does not start a new process per request; that would be a cluster or child-process setup.
- D. Node does not send 503 for this; the requests simply wait in the queue until the loop is free.
Tip: Synchronous code blocks the event loop in Node.js.
Question 4Easy
Why can a single Node.js process handle thousands of concurrent connections?
- Each connection is given its own dedicated JavaScript thread
- V8 runs every callback on a separate CPU core
- I/O is non-blocking, so the thread never waits idle on it
- Node queues connections and serves one at a time
Answer: C. I/O is non-blocking, so the thread never waits idle on it
Explanation
While waiting for network or disk, Node registers a callback and continues with other work, so one thread can juggle many in-flight requests.
Step-by-step solution
- Node.js uses non-blocking I/O operations.
- When I/O is in progress, the event loop handles callbacks instead of waiting.
- This allows a single thread to manage many connections without blocking.
- Each connection's callback runs when I/O completes.
- So the thread never waits idle on I/O.
Why the other options are wrong
- A. Node does not create a thread per connection; one event loop thread handles them all.
- B. V8 runs JavaScript callbacks on one main thread, not on a separate core for each callback.
- D. Node does not queue connections to serve one at a time; I/O is handled concurrently.
Tip: Non-blocking I/O lets one thread manage many connections without waiting.
Question 5Medium
What is the output of this script?
console.log(1);
setTimeout(() => console.log(2), 10);
setTimeout(() => console.log(3), 0);
Promise.resolve().then(() => console.log(4));
- 1 3 4 2
- 1 2 3 4
- 1 4 2 3
- 1 4 3 2
Answer: D. 1 4 3 2
Explanation
Sync code prints 1, the microtask prints 4, then the 0 ms timer (3) fires before the 10 ms timer (2).
Step-by-step solution
- console.log(1) runs first, synchronously.
- The promise callback 4 runs next as a microtask, before any timer.
- Timers run by delay: setTimeout 0 (prints 3) fires before setTimeout 10 (prints 2).
- Output is 1 4 3 2, option D.
Why the other options are wrong
- A. Promise callbacks run before timers, so 4 prints before 3.
- B. The promise 4 runs before both timers, so 4 cannot come after 2.
- C. Timers run after microtasks, and the 0 ms timer (3) fires before the 10 ms timer (2).
Tip: Synchronous, then microtasks, then timers in order of their delay.
Question 6Medium
What does the poll phase of the Node.js event loop mainly do?
- Run expired setTimeout and setInterval callbacks
- Fetch new I/O events and run their callbacks
- Run callbacks registered with setImmediate
- Run socket "close" event handlers
Answer: B. Fetch new I/O events and run their callbacks
Explanation
Poll retrieves completed I/O and runs its callbacks; if nothing else is scheduled it can block there waiting for I/O.
Step-by-step solution
- The poll phase waits for and collects new I/O events.
- It runs the callbacks for those events, such as incoming data or completed reads.
- So its main job is fetching I/O events and running their callbacks, option B.
Why the other options are wrong
- A. Expired timers are handled in the timers phase.
- C. SetImmediate callbacks run in the check phase.
- D. Close handlers run in the close callbacks phase.
Tip: Event loop phases: timers, pending, poll (I/O), check (setImmediate), close.
Question 7Medium
What is the output when this file runs as a CommonJS script (node main.js)?
Promise.resolve().then(() => console.log('promise'));
process.nextTick(() => console.log('tick'));
console.log('sync');
- sync promise tick
- sync tick promise
- tick sync promise
- promise tick sync
Answer: B. sync tick promise
Explanation
After the main script finishes, Node drains the process.nextTick queue before the promise microtask queue, so "tick" prints before "promise".
Step-by-step solution
- console.log('sync') runs first in the main script.
- process.nextTick callbacks run next, before promise callbacks in CommonJS.
- The promise callback runs last, so the output is sync tick promise, option B.
Why the other options are wrong
- A. In CommonJS the nextTick queue runs before promise callbacks.
- C. Sync always prints first, because it is synchronous code.
- D. Sync code and nextTick both run before the promise callback.
Tip: Node order: sync code, then process.nextTick queue, then promise microtasks, then timers.
Question 8Medium
In which event-loop phase do setImmediate() callbacks run?
- Check
- Timers
- Poll
- Close callbacks
Answer: A. Check
Explanation
The phases are timers, pending callbacks, idle/prepare, poll, check and close callbacks; setImmediate callbacks are queued for the check phase.
Step-by-step solution
- The event loop runs timers, pending callbacks, poll, check and close callbacks in turn.
- SetImmediate callbacks run in the check phase, right after poll.
- So the answer is the check phase, option A.
Why the other options are wrong
- B. The timers phase runs setTimeout and setInterval callbacks.
- C. The poll phase fetches I/O events; setImmediate runs after it in check.
- D. Close callbacks run in the close phase, for example socket close handlers.
Tip: setImmediate = check phase; setTimeout = timers phase.
Question 9Hard
What is the output of this script?
const fs = require('fs');
fs.readFile(__filename, () => {
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
});
- timeout immediate
- It varies between runs
- immediate timeout
- Only immediate is printed
Answer: C. immediate timeout
Explanation
Inside an I/O callback the loop is in the poll phase; the check phase (setImmediate) always comes before the loop wraps back round to the timers phase.
Step-by-step solution
- fs.readFile calls back in the poll phase of the event loop.
- Inside that callback, setImmediate runs in the check phase of the same loop turn.
- SetTimeout with 0 ms waits for the next timers phase, so immediate prints first, option C.
Why the other options are wrong
- A. Inside an I/O callback, setImmediate always runs before the next timer.
- B. Inside an I/O callback the order is fixed, so it does not vary between runs.
- D. Both callbacks run; timeout prints after immediate.
Tip: Inside an I/O callback, setImmediate runs before setTimeout 0. In the main module the order can vary.
Question 10Hard
A function keeps rescheduling itself with process.nextTick(fn) forever. What is the effect?
- It works like setImmediate and I/O stays responsive
- Node raises a stack overflow almost immediately
- Pending I/O callbacks are starved and never run
- Node detects the loop and drops extra ticks
Answer: C. Pending I/O callbacks are starved and never run
Explanation
The nextTick queue is drained completely before the loop moves on, so endless nextTick scheduling never lets the loop reach the poll phase. setImmediate would let I/O run between iterations.
Step-by-step solution
- process.nextTick callbacks run until the nextTick queue is empty, before the loop moves on.
- A function that reschedules itself with nextTick keeps the queue from ever being empty.
- The event loop never reaches the poll phase, so pending I/O callbacks are starved, option C.
Why the other options are wrong
- A. SetImmediate runs once per loop turn and lets I/O run; nextTick blocks the loop.
- B. No stack overflow happens, because each tick is queued rather than nested.
- D. Node does not detect or drop these ticks; the loop simply stays blocked.
Tip: Do not recurse with process.nextTick; use setImmediate to yield to I/O.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →