Node.js Technology & Digital Applications 3 — Questions and Answers
Question 1: In Express.js, what is the correct way to define a route parameter named `id`?
- /users/{id}
- /users/:id (Correct answer)
- /users/<id>
- /users/[id]
Correct answer: /users/:id
Express uses the colon syntax (`:id`) to declare route parameters, accessible via `req.params.id`.
Question 2: What will `typeof null` return in JavaScript/Node.js?
- "null"
- "undefined"
- "object" (Correct answer)
- "boolean"
Correct answer: "object"
`typeof null` returns `"object"` — a long-standing JavaScript bug that was kept for backward compatibility.
Question 3: Which npm command shows a tree of all installed packages and their dependencies for the current project?
- npm list (Correct answer)
- npm show
- npm deps
- npm tree
Correct answer: npm list
`npm list` (or `npm ls`) prints the dependency tree of the current project to the terminal.
Question 4: What is a key advantage of using `worker_threads` over the `child_process` module for CPU-intensive tasks?
- Worker threads can access the internet faster
- Worker threads share memory via SharedArrayBuffer, reducing data-copy overhead (Correct answer)
- Worker threads bypass the V8 engine for raw speed
- Worker threads automatically distribute work across multiple servers
Correct answer: Worker threads share memory via SharedArrayBuffer, reducing data-copy overhead
Worker threads run in the same process and can share memory through `SharedArrayBuffer`, making data transfer cheaper than IPC serialization.
Question 5: In Node.js, which event is emitted when all data has been read from a Readable stream?
- 'finish'
- 'close'
- 'end' (Correct answer)
- 'done'
Correct answer: 'end'
The `'end'` event is emitted when there is no more data to be consumed from a Readable stream.
Question 6: What does `process.nextTick(callback)` do in Node.js?
- Schedules the callback to run after the current I/O events but before the next event loop iteration (Correct answer)
- Schedules the callback to run after a 1ms delay
- Schedules the callback to run at the end of the current event loop iteration
- Schedules the callback to run in a worker thread
Correct answer: Schedules the callback to run after the current I/O events but before the next event loop iteration
`process.nextTick()` queues the callback to execute after the current operation completes, before the event loop continues to the next tick.
Question 7: Which of the following correctly reads the entire contents of a file asynchronously using the `fs` module's Promise API?
- fs.readFile(path)
- fs.promises.readFile(path, 'utf8') (Correct answer)
- fs.readFileSync(path, 'utf8')
- fs.open(path).then(read)
Correct answer: fs.promises.readFile(path, 'utf8')
`fs.promises.readFile()` returns a Promise that resolves with the file contents, enabling `async/await` usage without callbacks.
In Express.js, what is the correct way to define a route parameter named `id`?