
Most Node.js interviews begin with one topic: how Node runs your code. That means the single-threaded event loop, where libuv's thread pool comes in, and the order in which timers, promises and I/O callbacks run. This set covers those basics, plus modules, streams and the core APIs you use every day.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Topics covered in this set
- Event loop phases and the libuv thread pool
- process.nextTick, promises, setTimeout and setImmediate ordering
- CommonJS vs ES modules
- Streams, backpressure and pipe
- Buffers and binary data
- Worker threads, child processes and clustering
Node.js MCQs with answers and explanations
Q1. Which statement best describes how Node.js runs JavaScript?
a) Node.js runs JavaScript on all CPU cores automatically
b) Node.js blocks on every file read
c) JavaScript runs on a single main thread, and I/O is handled asynchronously by the operating system or the libuv thread pool
d) Each request gets its own JavaScript thread
Q2. What is the output order?
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("tick"));
console.log("sync");
a) sync, promise, tick, timeout
b) sync, tick, promise, timeout
c) sync, timeout, tick, promise
d) tick, sync, promise, timeout
Q3. Inside an I/O callback (for example in fs.readFile), which runs first: setImmediate or setTimeout(fn, 0)?
a) setImmediate, always
b) Neither runs
c) It is random
d) setTimeout, always
Q4. Why can overusing process.nextTick be dangerous?
a) Recursive nextTick calls can starve the event loop, because the nextTick queue is fully drained before I/O continues
b) It is deprecated
c) It only works in ES modules
d) It runs on a separate thread
Q5. What is a key difference between CommonJS and ES modules in Node.js?
a) ES modules use require
b) There is no difference
c) CommonJS require is synchronous and dynamic, while ES modules are statically analysable and support top-level await
d) CommonJS uses import and export
Q6. How do you tell Node.js to treat .js files in a package as ES modules?
a) Run node with --commonjs
b) Rename node_modules
c) Set "type": "module" in package.json
d) Add use strict at the top
Q7. Why are streams useful for serving a 2 GB file?
a) They process data in chunks, so memory use stays low instead of loading the whole file into memory
b) They encrypt the file
c) They are faster only for small files
d) They make the file smaller
Q8. What is backpressure in Node.js streams?
a) A security feature for HTTP
b) A way to compress data
c) An error thrown when a stream ends
d) A mechanism to slow down a fast readable source when the writable destination cannot keep up
Q9. Why is stream.pipeline() preferred over chaining .pipe() calls?
a) It forwards errors and cleans up all streams properly when one fails
b) It only works with files
c) It is synchronous
d) It disables backpressure
Q10. What is a Buffer in Node.js?
a) A JavaScript string with extra methods
b) A fixed-length sequence of raw bytes, used for binary data
c) A cache for HTTP responses
d) A queue of callbacks
Q11. A CPU-heavy task (for example resizing images) makes your Node.js API slow for everyone. What is the best fix?
a) Use setTimeout(fn, 0)
b) Increase the HTTP timeout
c) Move the work to worker threads or a separate service, so the main event loop stays free
d) Wrap the code in a Promise
Q12. What does the cluster module (or a process manager like PM2) do?
a) Splits a large array
b) Runs several Node.js processes that share a server port to use multiple CPU cores
c) Merges multiple files into one
d) Creates database clusters
Q13. Which module would you use to run a shell command such as git status from Node.js?
a) os
b) child_process
c) path
d) fs
Q14. What does EventEmitter provide?
a) Automatic retries
b) A way to emit named events and register listeners for them
c) Database connection pooling
d) HTTP routing
Q15. What happens when a promise rejection is not handled in current versions of Node.js?
a) It logs a warning only in development
b) The promise retries automatically
c) By default the process crashes with an unhandled rejection error
d) It is silently ignored
Quick revision notes
- Order: synchronous code, then the nextTick queue, then promise microtasks, then the loop phases (timers, poll, check and so on).
- Inside an I/O callback, setImmediate runs before setTimeout 0.
- Heavy CPU work blocks the event loop. Move it to worker threads or a separate service.
- Use streams and pipeline for large data, and respect backpressure.
- Unhandled promise rejections crash the process by default.
Frequently asked questions
Is Node.js single-threaded?
Your JavaScript runs on one main thread, but Node.js itself uses extra threads for the libuv thread pool, garbage collection and more. You can add your own threads with worker_threads.
When should I choose Node.js for a backend?
It fits well for services that mostly wait on I/O, like APIs, real-time apps and backends for frontends, more so if your team already writes JavaScript or TypeScript. For heavy CPU work, hand it to workers or another service.
What is the difference between process.nextTick and setImmediate?
nextTick runs right after the current operation, before the event loop moves on. setImmediate runs in the check phase of the loop. The names are confusing, but nextTick fires sooner.
Comments
Post a Comment