Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 2 MCQsImplementing HTTP Services & Basic Websites With Node.JS
Which built-in Node.js module is primarily used to create a web server?
Not quite. Try another option.
Answer: http
The 'http' module is the built-in Node.js module capable of creating an HTTP server.
Question 2 of 50
Which method of the HTTP module is used to initialize a server?
Not quite. Try another option.
Answer: http.createServer()
The createServer() method turns the computer into an HTTP server.
Question 3 of 50
What arguments does the request listener callback function in http.createServer() accept?
Not quite. Try another option.
Answer: (request, response)
The callback function receives two arguments: the incoming request object (req) and the outgoing response object (res).
Question 4 of 50
Which method is used to make the Node.js HTTP server accept connections on a specific port?
Not quite. Try another option.
Answer: server.listen()
The server.listen() method makes the server listen for incoming requests on a specified port.
Question 5 of 50
In a raw Node.js server, where is the request metadata (like headers and URL) stored?
Not quite. Try another option.
Answer: In the request object
The request object (req) contains information about the incoming HTTP request, including headers, URL, and method.
Question 6 of 50
Which property of the request object holds the part of the URL after the domain name?
Not quite. Try another option.
Answer: req.url
The req.url property holds the request URL string.
Question 7 of 50
Which method is used to send a chunk of the response body in the native HTTP module?
Not quite. Try another option.
Answer: res.write()
res.write() sends a chunk of the response body. It can be called multiple times before res.end().
Question 8 of 50
What must be called to complete the response and send it to the client in the native HTTP module?
Not quite. Try another option.
Answer: res.end()
res.end() signals to the server that all of the response headers and body have been sent.
Question 9 of 50
How do you set the HTTP status code to 404 using the native response object?
Not quite. Try another option.
Answer: res.statusCode = 404
In the native HTTP module, you set the statusCode property directly on the response object.
Question 10 of 50
Which method is used to write the status code and response headers simultaneously in native Node.js?
Not quite. Try another option.
Answer: res.writeHead()
res.writeHead(statusCode, [headers]) writes the status code and headers to the response stream.
Question 11 of 50
What is the correct HTTP status code for a successful request?
Not quite. Try another option.
Answer: 200
200 OK is the standard response for successful HTTP requests.
Question 12 of 50
What is the correct HTTP status code for 'Internal Server Error'?
Not quite. Try another option.
Answer: 500
500 represents a generic error message given when an unexpected condition was encountered on the server.
Question 13 of 50
When sending JSON data using native Node.js, what should the 'Content-Type' header be set to?
Not quite. Try another option.
Answer: application/json
The standard MIME type for JSON data is 'application/json'.
Question 14 of 50
How is basic routing typically implemented in a native Node.js HTTP server?
Not quite. Try another option.
Answer: Using if/else statements checking req.url
Native routing involves manually checking the req.url property and req.method inside conditional statements.
Question 15 of 50
Which HTTP method is used to request data from a specified resource?
Not quite. Try another option.
Answer: GET
The GET method is used to retrieve data from a server.
Question 16 of 50
Which HTTP method is commonly used to submit data to be processed to a specified resource?
Not quite. Try another option.
Answer: POST
The POST method is used to send data to a server to create/update a resource.
Question 17 of 50
What is Express.js?
Not quite. Try another option.
Answer: A fast, unopinionated web framework for Node.js
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
Question 18 of 50
Which command is used to install Express in a Node.js project?
Not quite. Try another option.
Answer: npm install express
npm (Node Package Manager) is used to install packages like Express.
Question 19 of 50
What is the first step to using Express in a file after installation?
Not quite. Try another option.
Answer: require('express')
You must require the module first: const express = require('express').
Question 20 of 50
How do you initialize an Express application?
Not quite. Try another option.
Answer: const app = express()
Calling the top-level express() function exported by the module creates an Express application.
Question 21 of 50
Which Express method is used to handle GET requests?
Not quite. Try another option.
Answer: app.get()
app.get() routes HTTP GET requests to the specified path with the specified callback functions.
Question 22 of 50
In Express, what method is used to send a response and automatically set the Content-Type?
Not quite. Try another option.
Answer: res.send()
res.send() sends the HTTP response. It automatically sets the Content-Type based on the body parameter type.
Question 23 of 50
How do you access route parameters (e.g., /users/:id) in Express?
Not quite. Try another option.
Answer: req.params
req.params contains properties mapped to the named route 'parameters'.
Question 24 of 50
How do you access query string parameters (e.g., ?search=term) in Express?
Not quite. Try another option.
Answer: req.query
req.query is an object containing a property for each query string parameter in the route.
Question 25 of 50
Which Express method starts the server listening for connections?
Not quite. Try another option.
Answer: app.listen()
Like the native http module, Express uses app.listen() to bind and listen for connections.
Question 26 of 50
What is the purpose of 'body-parser' middleware?
Not quite. Try another option.
Answer: To parse incoming request bodies
body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body.
Question 27 of 50
Before Express 4.16.0, body-parser was a separate install. How do you access JSON parsing now in modern Express?
Not quite. Try another option.
Answer: express.json()
Modern Express includes body-parser functionality via express.json() middleware.
Question 28 of 50
Which middleware parses incoming requests with URL-encoded payloads?
Not quite. Try another option.
Answer: express.urlencoded()
express.urlencoded() is a built-in middleware function in Express that parses incoming requests with urlencoded payloads.
Question 29 of 50
If body-parser is not used, what is the value of req.body for a POST request in Express by default?
Not quite. Try another option.
Answer: undefined
By default, req.body is undefined, and is populated only when you use body-parsing middleware.
Question 30 of 50
What does the extended: true option in urlencoded() middleware do?
Not quite. Try another option.
Answer: Allows parsing of rich objects and arrays
When set to true, the 'qs' library is used allows for a JSON-like experience with URL-encoded data.
Question 31 of 50
What is express.Router used for?
Not quite. Try another option.
Answer: To create modular, mountable route handlers
A Router instance is a complete middleware and routing system, often referred to as a 'mini-app', used to organize routes in separate files.
Question 32 of 50
How do you expose a router defined in a separate file?
Not quite. Try another option.
Answer: module.exports = router
In CommonJS (Node.js default), you export the router instance using module.exports so it can be required in other files.
Question 33 of 50
How do you mount a router module in the main app file?
Not quite. Try another option.
Answer: app.use(router)
app.use() is used to mount middleware functions or router instances at a specified path.
Question 34 of 50
If you mount a router at '/api', and the router has a route for '/users', what is the full path?
Not quite. Try another option.
Answer: /api/users
The paths are concatenated. The router is mounted at /api, so its internal /users route becomes /api/users.
Question 35 of 50
Which library is commonly used with Express for validating incoming request data?
Not quite. Try another option.
Answer: express-validator
express-validator is a set of express.js middlewares that wraps validator.js validator and sanitizer functions.
Question 36 of 50
In express-validator, which function is used to validate a field in the request body?
Not quite. Try another option.
Answer: body()
The body() function is used to validate fields specifically located in req.body.
Question 37 of 50
What does validationResult(req) return in express-validator?
Not quite. Try another option.
Answer: An object containing any validation errors
validationResult(req) extracts the validation errors from a request and makes them available in a result object.
Question 38 of 50
Which method in express-validator is used to check if a field is a valid email?
Not quite. Try another option.
Answer: isEmail()
isEmail() is the standard validator function to check if a string is an email.
Question 39 of 50
What is the purpose of sanitization in express-validator?
Not quite. Try another option.
Answer: To modify input data (e.g., trim whitespace)
Sanitization modifies the input to make it safe or standardized, such as trimming whitespace or escaping characters.
Question 40 of 50
Where do you place the validation middleware in an Express route definition?
Not quite. Try another option.
Answer: As an array of arguments before the callback
Validation chains are passed as middleware arguments to the route handler before the final controller callback.
Question 41 of 50
In express-validator, how do you check if the validation result contains errors?
Not quite. Try another option.
Answer: errors.isEmpty()
The result object from validationResult has an isEmpty() method that returns true if there are no errors.
Question 42 of 50
Which of the following matches a route path for ANY HTTP method in Express?
Not quite. Try another option.
Answer: app.all()
app.all() is a special routing method used to load middleware functions at a path for all HTTP request methods.
Question 43 of 50
What is a 'middleware' in the context of Express?
Not quite. Try another option.
Answer: Functions that have access to req, res, and next
Middleware functions are functions that have access to the request object, response object, and the next middleware function in the cycle.
Question 44 of 50
What does the next argument represent in an Express middleware function?
Not quite. Try another option.
Answer: A callback to pass control to the next middleware
Calling next() passes control to the next middleware function in the stack. If not called, the request will hang.
Question 45 of 50
Which status code indicates 'Bad Request' due to validation failure?
Not quite. Try another option.
Answer: 400
400 Bad Request is typically used when the server cannot process the request due to client error (e.g., malformed syntax, validation error).
Question 46 of 50
How do you chain multiple validators on the same field in express-validator?
Not quite. Try another option.
Answer: body('field').isEmail().normalizeEmail()
Validators and sanitizers can be chained method-style on the ValidationChain object.
Question 47 of 50
In a native Node server, how do you differentiate between a POST and a GET request?
Not quite. Try another option.
Answer: req.method
The req.method property contains the HTTP verb (e.g., 'GET', 'POST') as a string.
Question 48 of 50
What is the primary advantage of using Express over the native HTTP module?
Not quite. Try another option.
Answer: It simplifies routing and middleware management
Express provides a higher-level abstraction that makes defining routes, handling parameters, and managing middleware significantly easier than raw Node.
Question 49 of 50
Which method allows you to serve static files (images, CSS) in Express?
Not quite. Try another option.
Answer: express.static()
express.static(root, [options]) is a built-in middleware function in Express for serving static files.
Question 50 of 50
When setting a custom response header in Express, which method is used?
Not quite. Try another option.
Answer: res.set()
res.set(field, [value]) is used to set the response HTTP header field to value.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of INT222 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of INT222 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.