Sample questions
Question 1Easy
What does npm install express --save (or the default install) record in package.json?
- The full source code of express
- The user password
- Only the Node version
- The dependency name and version range
Answer: D. The dependency name and version range
Explanation
package.json lists dependencies; node_modules holds the downloaded code.
Step-by-step solution
- npm installs the package.
- package.json records the range.
Why the other options are wrong
- A. Code goes in node_modules.
- B. package.json holds no secrets by design.
- C. Dependencies are listed separately.
Question 2Easy
In Express, what does app.use(express.json()) do?
- Parses JSON bodies into req.body
- Sends JSON responses automatically
- Starts the server listening on a port
- Validates JSON schemas
Answer: A. Parses JSON bodies into req.body
Explanation
The middleware reads the body, parses JSON and stores the object on req.body.
Step-by-step solution
- Middleware runs before routes.
- It fills req.body.
Why the other options are wrong
- B. res.json does that.
- C. app.listen does that.
- D. It only parses the body.
Question 3Easy
What is middleware in Express?
- A function taking req, res and next that runs per request
- A database driver
- A template engine
- A package manager
Answer: A. A function taking req, res and next that runs per request
Explanation
Middleware functions can modify the request or response, end it, or call next() to pass control on.
Step-by-step solution
- Receives req, res, next.
- Calls next() to continue.
Why the other options are wrong
- B. Middleware is not a driver.
- C. Template engines render views.
- D. npm manages packages.
Question 4Medium
What does process.env.PORT commonly hold in a Node app?
- A global array of all open ports
- An environment variable, such as the listening port
- The hostname of the database
- A built-in constant of Node
Answer: B. An environment variable, such as the listening port
Explanation
Configuration such as ports and secrets is read from environment variables so it can differ per deployment.
Step-by-step solution
- process.env exposes env vars.
- Use PORT for deployment flexibility.
Why the other options are wrong
- A. It is an environment variable.
- C. Only if configured; PORT is the listening port.
- D. Environment values are set by the system.
Question 5Medium
An Express middleware neither sends a response nor calls next(). What happens to the request?
- Express automatically calls the next middleware
- It hangs until it times out
- Express sends a 200 empty response
- The server crashes immediately
Answer: B. It hangs until it times out
Explanation
The chain only advances when next() is called, otherwise the client waits.
Step-by-step solution
- No response.
- No next().
- Client times out.
Why the other options are wrong
- A. Express waits for next().
- C. There is no automatic reply.
- D. The request just stalls.
Question 6Medium
What is the main reason Node.js can handle many concurrent connections with one thread?
- It spawns a new OS thread for every request
- Non-blocking I/O with an event loop
- It compiles JavaScript to run in parallel on all cores automatically
- It forbids network calls
Answer: B. Non-blocking I/O with an event loop
Explanation
I/O operations are handed off and their callbacks run on the event loop when ready, so the thread is not blocked waiting.
Step-by-step solution
- I/O is delegated.
- Event loop runs callbacks.
Why the other options are wrong
- A. Node uses an event loop with a small thread pool.
- C. A single process runs JS on one thread.
- D. Network calls are central to Node.
Question 7Medium
Which Express response correctly returns JSON with status 201?
- res.send(201).json({ ok: true })
- res.json(201, { ok: true })
- res.sendStatus({ ok: true })
- res.status(201).json({ ok: true })
Answer: D. res.status(201).json({ ok: true })
Explanation
status() sets the code and returns the response so json() can follow.
Step-by-step solution
- Set status first.
- Chain json().
Why the other options are wrong
- A. send(201) sends a body; chaining json afterwards fails.
- B. The numeric-status overload is removed or deprecated.
- C. sendStatus takes a status code.
Question 8Medium
Which route reads the id from a URL like /users/42 in Express?
- app.get("/users", (req, res) => res.send(req.body.id))
- app.get("/users/id", (req, res) => res.send(req.id))
- app.get("/users?", (req, res) => res.send(req.query))
- app.get("/users/:id", (req, res) => res.send(req.params.id))
Answer: D. app.get("/users/:id", (req, res) => res.send(req.params.id))
Explanation
A :id segment becomes req.params.id, a string "42".
Step-by-step solution
- :id marks a route parameter.
- Read it from req.params.
Why the other options are wrong
- A. req.body is for request bodies.
- B. There is no req.id.
- C. The path pattern does not capture 42.
Question 9Hard
In Express, where must a global error-handling middleware be placed, and what is its signature?
- Last, with four parameters (err, req, res, next)
- Before all routes, with two parameters
- Anywhere, with three parameters
- Inside each route only
Answer: A. Last, with four parameters (err, req, res, next)
Explanation
Express identifies error handlers by their four arguments and runs them for errors passed via next(err).
Step-by-step solution
- Four params marks an error handler.
- Register it last.
Why the other options are wrong
- B. It would not catch route errors.
- C. The four-parameter form marks error handlers.
- D. It is registered once on the app.
Question 10Hard
A Node API runs a heavy CPU loop inside a request handler. What is the effect on other requests?
- Nothing, Node runs each handler in its own thread
- Only requests to the same route are delayed
- They wait, because the one JS thread is blocked
- The loop is automatically moved to the GPU
Answer: C. They wait, because the one JS thread is blocked
Explanation
CPU-bound work blocks the event loop; offload it to worker threads or another process.
Step-by-step solution
- One thread runs JS.
- The loop blocks it.
- Other requests wait.
Why the other options are wrong
- A. JS code runs on one thread.
- B. All requests share the same thread.
- D. Node does no such thing.
Practise all 10 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 →