1
Which built-in Node.js module is primarily used to create a web server?
A. url
B. http
C. fs
D. server
Reveal Answer
Hide Answer
Correct Answer: http
Explanation:
The 'http' module is the built-in Node.js module capable of creating an HTTP server.
2
Which method of the HTTP module is used to initialize a server?
A. http.initServer()
B. http.startServer()
C. http.createServer()
D. http.newServer()
Reveal Answer
Hide Answer
Correct Answer: http.createServer()
Explanation:
The createServer() method turns the computer into an HTTP server.
3
What arguments does the request listener callback function in http.createServer() accept?
A. (req, data)
B. (response, error)
C. (data, status)
D. (request, response)
Reveal Answer
Hide Answer
Correct Answer: (request, response)
Explanation:
The callback function receives two arguments: the incoming request object (req) and the outgoing response object (res).
4
Which method is used to make the Node.js HTTP server accept connections on a specific port?
A. server.listen()
B. server.bind()
C. server.run()
D. server.start()
Reveal Answer
Hide Answer
Correct Answer: server.listen()
Explanation:
The server.listen() method makes the server listen for incoming requests on a specified port.
5
In a raw Node.js server, where is the request metadata (like headers and URL) stored?
A. In the global scope
B. In the request object
C. In the file system
D. In the response object
Reveal Answer
Hide Answer
Correct Answer: In the request object
Explanation:
The request object (req) contains information about the incoming HTTP request, including headers, URL, and method.
6
Which property of the request object holds the part of the URL after the domain name?
A. req.href
B. req.link
C. req.path
D. req.url
Reveal Answer
Hide Answer
Correct Answer: req.url
Explanation:
The req.url property holds the request URL string.
7
Which method is used to send a chunk of the response body in the native HTTP module?
A. res.write()
B. res.log()
C. res.push()
D. res.send()
Reveal Answer
Hide Answer
Correct Answer: res.write()
Explanation:
res.write() sends a chunk of the response body. It can be called multiple times before res.end().
8
What must be called to complete the response and send it to the client in the native HTTP module?
A. res.end()
B. res.complete()
C. res.stop()
D. res.finish()
Reveal Answer
Hide Answer
Correct Answer: res.end()
Explanation:
res.end() signals to the server that all of the response headers and body have been sent.
9
How do you set the HTTP status code to 404 using the native response object?
A. res.status(404)
B. res.setStatus(404)
C. res.statusCode = 404
D. res.code = 404
Reveal Answer
Hide Answer
Correct Answer: res.statusCode = 404
Explanation:
In the native HTTP module, you set the statusCode property directly on the response object.
10
Which method is used to write the status code and response headers simultaneously in native Node.js?
A. res.writeHead()
B. res.sendHeader()
C. res.writeHeader()
D. res.setHeader()
Reveal Answer
Hide Answer
Correct Answer: res.writeHead()
Explanation:
res.writeHead(statusCode, [headers]) writes the status code and headers to the response stream.
11
What is the correct HTTP status code for a successful request?
A. 201
B. 400
C. 500
D. 200
Reveal Answer
Hide Answer
Correct Answer: 200
Explanation:
200 OK is the standard response for successful HTTP requests.
12
What is the correct HTTP status code for 'Internal Server Error'?
A. 500
B. 404
C. 403
D. 301
Reveal Answer
Hide Answer
Correct Answer: 500
Explanation:
500 represents a generic error message given when an unexpected condition was encountered on the server.
13
When sending JSON data using native Node.js, what should the 'Content-Type' header be set to?
A. application/json
B. text/json
C. application/javascript
D. text/html
Reveal Answer
Hide Answer
Correct Answer: application/json
Explanation:
The standard MIME type for JSON data is 'application/json'.
14
How is basic routing typically implemented in a native Node.js HTTP server?
A. Using if/else statements checking req.url
B. Using switch/case on req.body
C. Using a router file
D. It handles routing automatically
Reveal Answer
Hide Answer
Correct Answer: Using if/else statements checking req.url
Explanation:
Native routing involves manually checking the req.url property and req.method inside conditional statements.
15
Which HTTP method is used to request data from a specified resource?
A. PUT
B. POST
C. DELETE
D. GET
Reveal Answer
Hide Answer
Correct Answer: GET
Explanation:
The GET method is used to retrieve data from a server.
16
Which HTTP method is commonly used to submit data to be processed to a specified resource?
A. HEAD
B. POST
C. GET
D. OPTIONS
Reveal Answer
Hide Answer
Correct Answer: POST
Explanation:
The POST method is used to send data to a server to create/update a resource.
17
What is Express.js?
A. A database for Node.js
B. A front-end library
C. A strict MVC framework
D. A fast, unopinionated web framework for Node.js
Reveal Answer
Hide Answer
Correct Answer: A fast, unopinionated web framework for Node.js
Explanation:
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
18
Which command is used to install Express in a Node.js project?
A. npm install express
B. install express
C. node install express
D. npm get express
Reveal Answer
Hide Answer
Correct Answer: npm install express
Explanation:
npm (Node Package Manager) is used to install packages like Express.
19
What is the first step to using Express in a file after installation?
A. import express from 'node'
B. start express
C. const app = express()
D. require('express')
Reveal Answer
Hide Answer
Correct Answer: require('express')
Explanation:
You must require the module first: const express = require('express').
20
How do you initialize an Express application?
A. const app = start()
B. const app = new Express()
C. const app = express()
D. const app = createApplication()
Reveal Answer
Hide Answer
Correct Answer: const app = express()
Explanation:
Calling the top-level express() function exported by the module creates an Express application.
21
Which Express method is used to handle GET requests?
A. app.fetch()
B. app.get()
C. app.retrieve()
D. app.read()
Reveal Answer
Hide Answer
Correct Answer: app.get()
Explanation:
app.get() routes HTTP GET requests to the specified path with the specified callback functions.
22
In Express, what method is used to send a response and automatically set the Content-Type?
A. res.send()
B. res.dispatch()
C. res.write()
D. res.emit()
Reveal Answer
Hide Answer
Correct Answer: res.send()
Explanation:
res.send() sends the HTTP response. It automatically sets the Content-Type based on the body parameter type.
23
How do you access route parameters (e.g., /users/:id) in Express?
A. req.query
B. req.body
C. req.params
D. req.route
Reveal Answer
Hide Answer
Correct Answer: req.params
Explanation:
req.params contains properties mapped to the named route 'parameters'.
24
How do you access query string parameters (e.g., ?search=term) in Express?
A. req.params
B. req.query
C. req.url
D. req.search
Reveal Answer
Hide Answer
Correct Answer: req.query
Explanation:
req.query is an object containing a property for each query string parameter in the route.
25
Which Express method starts the server listening for connections?
A. app.init()
B. app.start()
C. app.listen()
D. app.run()
Reveal Answer
Hide Answer
Correct Answer: app.listen()
Explanation:
Like the native http module, Express uses app.listen() to bind and listen for connections.
26
What is the purpose of 'body-parser' middleware?
A. To parse the URL
B. To send JSON data
C. To validate the response
D. To parse incoming request bodies
Reveal Answer
Hide Answer
Correct Answer: To parse incoming request bodies
Explanation:
body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body.
27
Before Express 4.16.0, body-parser was a separate install. How do you access JSON parsing now in modern Express?
A. express.parse()
B. express.body()
C. It is not supported
D. express.json()
Reveal Answer
Hide Answer
Correct Answer: express.json()
Explanation:
Modern Express includes body-parser functionality via express.json() middleware.
28
Which middleware parses incoming requests with URL-encoded payloads?
A. express.text()
B. express.raw()
C. express.form()
D. express.urlencoded()
Reveal Answer
Hide Answer
Correct Answer: express.urlencoded()
Explanation:
express.urlencoded() is a built-in middleware function in Express that parses incoming requests with urlencoded payloads.
29
If body-parser is not used, what is the value of req.body for a POST request in Express by default?
A. null
B. An empty object {}
C. undefined
D. An empty string
Reveal Answer
Hide Answer
Correct Answer: undefined
Explanation:
By default, req.body is undefined, and is populated only when you use body-parsing middleware.
30
What does the extended: true option in urlencoded() middleware do?
A. Increases the body size limit
B. Enables HTTPS
C. Allows parsing of rich objects and arrays
D. Parses XML data
Reveal Answer
Hide Answer
Correct Answer: Allows parsing of rich objects and arrays
Explanation:
When set to true, the 'qs' library is used allows for a JSON-like experience with URL-encoded data.
31
What is express.Router used for?
A. To connect to the internet
B. To route data to the database
C. To handle static files
D. To create modular, mountable route handlers
Reveal Answer
Hide Answer
Correct Answer: To create modular, mountable route handlers
Explanation:
A Router instance is a complete middleware and routing system, often referred to as a 'mini-app', used to organize routes in separate files.
32
How do you expose a router defined in a separate file?
A. module.exports = router
B. export default router
C. exports.router = router
D. return router
Reveal Answer
Hide Answer
Correct Answer: module.exports = router
Explanation:
In CommonJS (Node.js default), you export the router instance using module.exports so it can be required in other files.
33
How do you mount a router module in the main app file?
A. app.add(router)
B. app.route(router)
C. app.use(router)
D. app.mount(router)
Reveal Answer
Hide Answer
Correct Answer: app.use(router)
Explanation:
app.use() is used to mount middleware functions or router instances at a specified path.
34
If you mount a router at '/api', and the router has a route for '/users', what is the full path?
A. /api/users
B. /users
C. /api
D. /users/api
Reveal Answer
Hide Answer
Correct Answer: /api/users
Explanation:
The paths are concatenated. The router is mounted at /api, so its internal /users route becomes /api/users.
35
Which library is commonly used with Express for validating incoming request data?
A. express-validator
B. express-check
C. node-validate
D. body-check
Reveal Answer
Hide Answer
Correct Answer: express-validator
Explanation:
express-validator is a set of express.js middlewares that wraps validator.js validator and sanitizer functions.
36
In express-validator, which function is used to validate a field in the request body?
A. req.check()
B. checkBody()
C. body()
D. validate()
Reveal Answer
Hide Answer
Correct Answer: body()
Explanation:
The body() function is used to validate fields specifically located in req.body.
37
What does validationResult(req) return in express-validator?
A. Boolean true/false
B. The response object
C. The sanitized body
D. An object containing any validation errors
Reveal Answer
Hide Answer
Correct Answer: An object containing any validation errors
Explanation:
validationResult(req) extracts the validation errors from a request and makes them available in a result object.
38
Which method in express-validator is used to check if a field is a valid email?
A. isEmail()
B. isValidEmail()
C. validateEmail()
D. checkEmail()
Reveal Answer
Hide Answer
Correct Answer: isEmail()
Explanation:
isEmail() is the standard validator function to check if a string is an email.
39
What is the purpose of sanitization in express-validator?
A. To log data
B. To modify input data (e.g., trim whitespace)
C. To reject invalid data
D. To encrypt data
Reveal Answer
Hide Answer
Correct Answer: To modify input data (e.g., trim whitespace)
Explanation:
Sanitization modifies the input to make it safe or standardized, such as trimming whitespace or escaping characters.
40
Where do you place the validation middleware in an Express route definition?
A. In the app.listen() method
B. As an array of arguments before the callback
C. In the package.json
D. Inside the callback function
Reveal Answer
Hide Answer
Correct Answer: As an array of arguments before the callback
Explanation:
Validation chains are passed as middleware arguments to the route handler before the final controller callback.
41
In express-validator, how do you check if the validation result contains errors?
A. errors.hasErrors()
B. errors.length > 0
C. errors.exist()
D. errors.isEmpty()
Reveal Answer
Hide Answer
Correct Answer: errors.isEmpty()
Explanation:
The result object from validationResult has an isEmpty() method that returns true if there are no errors.
42
Which of the following matches a route path for ANY HTTP method in Express?
A. app.any()
B. app.star()
C. app.every()
D. app.all()
Reveal Answer
Hide Answer
Correct Answer: app.all()
Explanation:
app.all() is a special routing method used to load middleware functions at a path for all HTTP request methods.
43
What is a 'middleware' in the context of Express?
A. Functions that have access to req, res, and next
B. The front-end framework
C. The database layer
D. Hardware drivers
Reveal Answer
Hide Answer
Correct Answer: Functions that have access to req, res, and next
Explanation:
Middleware functions are functions that have access to the request object, response object, and the next middleware function in the cycle.
44
What does the next argument represent in an Express middleware function?
A. The previous request
B. The database connection
C. A callback to pass control to the next middleware
D. The error object
Reveal Answer
Hide Answer
Correct Answer: A callback to pass control to the next middleware
Explanation:
Calling next() passes control to the next middleware function in the stack. If not called, the request will hang.
45
Which status code indicates 'Bad Request' due to validation failure?
A. 404
B. 400
C. 503
D. 200
Reveal Answer
Hide Answer
Correct Answer: 400
Explanation:
400 Bad Request is typically used when the server cannot process the request due to client error (e.g., malformed syntax, validation error).
46
How do you chain multiple validators on the same field in express-validator?
A. body('field').isEmail().normalizeEmail()
B. body('field', 'isEmail', 'normalizeEmail')
C. You cannot chain them
D. validate('field', [isEmail, normalizeEmail])
Reveal Answer
Hide Answer
Correct Answer: body('field').isEmail().normalizeEmail()
Explanation:
Validators and sanitizers can be chained method-style on the ValidationChain object.
47
In a native Node server, how do you differentiate between a POST and a GET request?
A. req.type
B. req.method
C. req.action
D. req.verb
Reveal Answer
Hide Answer
Correct Answer: req.method
Explanation:
The req.method property contains the HTTP verb (e.g., 'GET', 'POST') as a string.
48
What is the primary advantage of using Express over the native HTTP module?
A. It simplifies routing and middleware management
B. It removes the need for JavaScript
C. It allows using Java
D. It is slower but more secure
Reveal Answer
Hide Answer
Correct Answer: It simplifies routing and middleware management
Explanation:
Express provides a higher-level abstraction that makes defining routes, handling parameters, and managing middleware significantly easier than raw Node.
49
Which method allows you to serve static files (images, CSS) in Express?
A. express.assets()
B. express.files()
C. express.public()
D. express.static()
Reveal Answer
Hide Answer
Correct Answer: express.static()
Explanation:
express.static(root, [options]) is a built-in middleware function in Express for serving static files.
50
When setting a custom response header in Express, which method is used?
A. res.put()
B. res.set()
C. res.config()
D. res.addHeader()
Reveal Answer
Hide Answer
Correct Answer: res.set()
Explanation:
res.set(field, [value]) is used to set the response HTTP header field to value.