Unit 2 - Practice Quiz

INT222

1 Which built-in Node.js module is primarily used to create a web server?

A. fs
B. http
C. url
D. server

2 Which method of the HTTP module is used to initialize a server?

A. http.initServer()
B. http.createServer()
C. http.startServer()
D. http.newServer()

3 What arguments does the request listener callback function in http.createServer() accept?

A. (request, response)
B. (req, data)
C. (response, error)
D. (data, status)

4 Which method is used to make the Node.js HTTP server accept connections on a specific port?

A. server.start()
B. server.run()
C. server.listen()
D. server.bind()

5 In a raw Node.js server, where is the request metadata (like headers and URL) stored?

A. In the response object
B. In the global scope
C. In the request object
D. In the file system

6 Which property of the request object holds the part of the URL after the domain name?

A. req.path
B. req.url
C. req.href
D. req.link

7 Which method is used to send a chunk of the response body in the native HTTP module?

A. res.send()
B. res.write()
C. res.push()
D. res.log()

8 What must be called to complete the response and send it to the client in the native HTTP module?

A. res.stop()
B. res.finish()
C. res.end()
D. res.complete()

9 How do you set the HTTP status code to 404 using the native response object?

A. res.code = 404
B. res.statusCode = 404
C. res.setStatus(404)
D. res.status(404)

10 Which method is used to write the status code and response headers simultaneously in native Node.js?

A. res.writeHead()
B. res.setHeader()
C. res.sendHeader()
D. res.writeHeader()

11 What is the correct HTTP status code for a successful request?

A. 200
B. 201
C. 400
D. 500

12 What is the correct HTTP status code for 'Internal Server Error'?

A. 404
B. 500
C. 301
D. 403

13 When sending JSON data using native Node.js, what should the 'Content-Type' header be set to?

A. text/html
B. text/json
C. application/json
D. application/javascript

14 How is basic routing typically implemented in a native Node.js HTTP server?

A. Using a router file
B. Using if/else statements checking req.url
C. Using switch/case on req.body
D. It handles routing automatically

15 Which HTTP method is used to request data from a specified resource?

A. POST
B. PUT
C. DELETE
D. GET

16 Which HTTP method is commonly used to submit data to be processed to a specified resource?

A. GET
B. POST
C. HEAD
D. OPTIONS

17 What is Express.js?

A. A database for Node.js
B. A fast, unopinionated web framework for Node.js
C. A front-end library
D. A strict MVC framework

18 Which command is used to install Express in a Node.js project?

A. node install express
B. npm install express
C. npm get express
D. install express

19 What is the first step to using Express in a file after installation?

A. const app = express()
B. require('express')
C. import express from 'node'
D. start express

20 How do you initialize an Express application?

A. const app = new Express()
B. const app = express()
C. const app = createApplication()
D. const app = start()

21 Which Express method is used to handle GET requests?

A. app.fetch()
B. app.retrieve()
C. app.get()
D. app.read()

22 In Express, what method is used to send a response and automatically set the Content-Type?

A. res.write()
B. res.send()
C. res.emit()
D. res.dispatch()

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

24 How do you access query string parameters (e.g., ?search=term) in Express?

A. req.params
B. req.query
C. req.search
D. req.url

25 Which Express method starts the server listening for connections?

A. app.run()
B. app.listen()
C. app.start()
D. app.init()

26 What is the purpose of 'body-parser' middleware?

A. To parse the URL
B. To validate the response
C. To parse incoming request bodies
D. To send JSON data

27 Before Express 4.16.0, body-parser was a separate install. How do you access JSON parsing now in modern Express?

A. express.body()
B. express.json()
C. express.parse()
D. It is not supported

28 Which middleware parses incoming requests with URL-encoded payloads?

A. express.urlencoded()
B. express.text()
C. express.raw()
D. express.form()

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. undefined
C. An empty object {}
D. An empty string

30 What does the extended: true option in urlencoded() middleware do?

A. Allows parsing of rich objects and arrays
B. Enables HTTPS
C. Increases the body size limit
D. Parses XML data

31 What is express.Router used for?

A. To connect to the internet
B. To create modular, mountable route handlers
C. To route data to the database
D. To handle static files

32 How do you expose a router defined in a separate file?

A. exports.router = router
B. module.exports = router
C. return router
D. export default router

33 How do you mount a router module in the main app file?

A. app.add(router)
B. app.use(router)
C. app.route(router)
D. app.mount(router)

34 If you mount a router at '/api', and the router has a route for '/users', what is the full path?

A. /users
B. /api
C. /api/users
D. /users/api

35 Which library is commonly used with Express for validating incoming request data?

A. express-check
B. express-validator
C. body-check
D. node-validate

36 In express-validator, which function is used to validate a field in the request body?

A. checkBody()
B. body()
C. validate()
D. req.check()

37 What does validationResult(req) return in express-validator?

A. Boolean true/false
B. The sanitized body
C. An object containing any validation errors
D. The response object

38 Which method in express-validator is used to check if a field is a valid email?

A. isEmail()
B. checkEmail()
C. validateEmail()
D. isValidEmail()

39 What is the purpose of sanitization in express-validator?

A. To reject invalid data
B. To encrypt data
C. To modify input data (e.g., trim whitespace)
D. To log data

40 Where do you place the validation middleware in an Express route definition?

A. Inside the callback function
B. As an array of arguments before the callback
C. In the app.listen() method
D. In the package.json

41 In express-validator, how do you check if the validation result contains errors?

A. errors.exist()
B. errors.isEmpty()
C. errors.hasErrors()
D. errors.length > 0

42 Which of the following matches a route path for ANY HTTP method in Express?

A. app.any()
B. app.all()
C. app.every()
D. app.star()

43 What is a 'middleware' in the context of Express?

A. Hardware drivers
B. Functions that have access to req, res, and next
C. The database layer
D. The front-end framework

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

45 Which status code indicates 'Bad Request' due to validation failure?

A. 200
B. 400
C. 404
D. 503

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. validate('field', [isEmail, normalizeEmail])
D. You cannot chain them

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

48 What is the primary advantage of using Express over the native HTTP module?

A. It allows using Java
B. It removes the need for JavaScript
C. It simplifies routing and middleware management
D. It is slower but more secure

49 Which method allows you to serve static files (images, CSS) in Express?

A. express.static()
B. express.files()
C. express.public()
D. express.assets()

50 When setting a custom response header in Express, which method is used?

A. res.addHeader()
B. res.set()
C. res.put()
D. res.config()