Unit 2 - Practice Quiz

INT222 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

A. url
B. http
C. fs
D. 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()

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)

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()

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

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

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()

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()

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

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()

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

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

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

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

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

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

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

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

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

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

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

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')

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()

21 Which Express method is used to handle GET requests?

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

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()

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.url
D. req.search

25 Which Express method starts the server listening for connections?

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

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

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()

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

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

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

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

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

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

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)

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

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

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()

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

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()

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

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

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()

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()

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

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. 404
B. 400
C. 503
D. 200

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])

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 simplifies routing and middleware management
B. It removes the need for JavaScript
C. It allows using Java
D. It is slower but more secure

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()

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()