JavaScript Node.js Basics 3 — Questions and Answers
Question 1: What is the Node.js event loop's primary role?
- To spawn child processes for CPU-intensive tasks
- To manage the execution of asynchronous callbacks by monitoring the call stack and callback queue (Correct answer)
- To compile JavaScript into native machine code
- To handle garbage collection of unused variables
Correct answer: To manage the execution of asynchronous callbacks by monitoring the call stack and callback queue
The event loop continuously checks whether the call stack is empty and, if so, pushes the next callback from the queue onto the stack for execution.
Question 2: Which of the following is NOT a valid phase of the Node.js event loop?
- timers
- poll
- compile (Correct answer)
- check
Correct answer: compile
The event loop phases are timers, pending callbacks, idle/prepare, poll, check, and close callbacks — there is no 'compile' phase.
Question 3: What does `setImmediate()` do in Node.js?
- Executes a callback synchronously before the current operation finishes
- Schedules a callback to run after I/O events in the current iteration of the event loop (Correct answer)
- Executes a callback after a fixed delay in milliseconds
- Schedules a callback to run before any timers in the next tick
Correct answer: Schedules a callback to run after I/O events in the current iteration of the event loop
`setImmediate()` schedules its callback in the `check` phase, which runs after I/O callbacks in the same event loop iteration.
Question 4: What is `process.nextTick()` used for in Node.js?
- To defer execution until the next event loop iteration
- To schedule a callback to run before any I/O events but after the current operation completes (Correct answer)
- To pause execution for one tick of the system clock
- To fire a callback after all setImmediate callbacks
Correct answer: To schedule a callback to run before any I/O events but after the current operation completes
`process.nextTick()` queues a callback in the nextTick queue, which is drained before the event loop continues to any other phase.
Question 5: In Node.js, which module is used to create child processes?
- threads
- worker
- child_process (Correct answer)
- subprocess
Correct answer: child_process
The built-in `child_process` module provides `spawn()`, `exec()`, `execFile()`, and `fork()` to create and communicate with child processes.
Question 6: What happens when you call `EventEmitter.emit('error')` and no 'error' listener is registered?
- The error event is silently ignored
- A warning is printed to stderr but execution continues
- Node.js throws an unhandled error and may crash the process (Correct answer)
- The event is buffered until a listener is added
Correct answer: Node.js throws an unhandled error and may crash the process
Emitting an 'error' event on an EventEmitter with no registered error listener causes Node.js to throw the error, potentially crashing the process.
Question 7: What does the `cluster` module in Node.js enable?
- Database connection pooling across multiple queries
- Spawning multiple worker processes that share the same server port (Correct answer)
- Grouping npm packages into install clusters
- Load-balancing HTTP requests using a round-robin DNS
Correct answer: Spawning multiple worker processes that share the same server port
The `cluster` module lets you fork worker processes that all listen on the same port, enabling multi-core utilization for Node.js servers.
What is the Node.js event loop's primary role?