Express.js Quiz - MCQ - Multiple Choice Questions

Express.js, a minimalist web framework for Node.js, has become an essential tool for many web developers. This quiz is designed to test your foundational knowledge of Express.js. For each question, choose the best answer. Explanations are provided for each correct answer to help you understand the concepts better.

1. What is Express.js?

A. A database
B. A frontend framework
C. A web server framework for Node.js
D. An operating system

Answer:

C. A web server framework for Node.js

Explanation:

Express.js is a minimalist web server framework for Node.js that simplifies the process of building web applications.

2. Which method sends a JSON response in Express.js?

A. res.json()
B. res.send()
C. res.sendFile()
D. res.render()

Answer:

A. res.json()

Explanation:

The res.json() method sends a JSON response.

3. How do you capture query parameters in Express routes?

A. req.data
B. req.params
C. req.query
D. req.body

Answer:

C. req.query

Explanation:

Query parameters are captured using the req.query object.

4. Which of these is not a core feature of Express?

A. Middleware
B. Routing
C. Templating
D. ORM

Answer:

D. ORM

Explanation:

While Express provides middleware, routing, and templating, it does not have an in-built Object-Relational Mapping (ORM) system.

5. To serve static files like images, CSS files, and JavaScript files in Express, you use:

A. express.static()
B. res.sendFile()
C. app.static()
D. res.sendStatic()

Answer:

A. express.static()

Explanation:

The express.static() middleware function is used to serve static files.

6. In Express.js, how do you redirect a user to another page?

A. res.send('/new-url')
B. res.redirect('/new-url')
C. res.goto('/new-url')
D. app.redirect('/new-url')

Answer:

B. res.redirect('/new-url')

Explanation:

The res.redirect() method is used to redirect the user to another URL.

7. What's the primary use of middleware in Express?

A. Data storage
B. Error handling
C. Execution of functions in the request-response cycle
D. Frontend rendering

Answer:

C. Execution of functions in the request-response cycle

Explanation:

Middleware functions have access to the request and response objects and are used to execute any code, end the request-response cycle, or invoke the next middleware in the stack.

8. What method is used to create a middleware in Express?

A. app.middleware()
B. app.use()
C. express.middleware()
D. express.use()

Answer:

B. app.use()

Explanation:

The app.use() method is used to mount a middleware function.

9. How do you send a status of 404 and a message 'Not Found' in Express?

A. res.status(404).message('Not Found')
B. res.status(404).send('Not Found')
C. res.send(404, 'Not Found')
D. res.sendStatus(404, 'Not Found')

Answer:

B. res.status(404).send('Not Found')

Explanation:

The res.status().send() combination allows you to set a specific status code and send a message.

10. Which of these methods will handle HTTP POST requests in an Express app?

A. app.post()
B. app.get()
C. app.all()
D. app.put()

Answer:

A. app.post()

Explanation:

The app.post() method is specifically designed to handle POST requests.

11. How do you capture data sent in the body of a POST request in Express?

A. req.params
B. req.body
C. req.data
D. req.query

Answer:

B. req.body

Explanation:

req.body contains data sent in the body of the request, typically used for POST data.

12. To use req.body, what middleware must be used with Express?

A. express.urlencoded()
B. express.query()
C. express.static()
D. express.getbody()

Answer:

A. express.urlencoded()

Explanation:

express.urlencoded() is a middleware that parses incoming requests with URL-encoded payloads, thus populating req.body.

13. Which middleware in Express.js helps in handling JSON data from POST requests?

A. express.json()
B. express.urlencoded()
C. express.parse()
D. express.data()

Answer:

A. express.json()

Explanation:

express.json() is a built-in middleware in Express.js that parses incoming requests with JSON payloads.

14. Which of the following methods is used to render a view in Express.js?

A. res.view()
B. res.page()
C. res.sendView()
D. res.render()

Answer:

D. res.render()

Explanation:

res.render() is used to render a view template.

15. If you want to set a cookie in Express.js, which method would you use?

A. res.setCookie()
B. res.cookie()
C. res.saveCookie()
D. res.putCookie()

Answer:

B. res.cookie()

Explanation:

The res.cookie() method is used to set cookies.

16. In Express, if you wanted a middleware function to be executed for every request, where would you place it?

A. At the end of all route definitions
B. Before all route definitions
C. Inside each route definition
D. Inside the app's main file

Answer:

B. Before all route definitions

Explanation:

Placing the middleware before all route definitions ensures it gets executed for every request before specific route logic is handled.

17. What will next() function do in Express middleware?

A. It will end the response cycle.
B. It will pass control to the next middleware function.
C. It will restart the request-response cycle.
D. It will throw an error.

Answer:

B. It will pass control to the next middleware function.

Explanation:

In Express, the next() function, when called within middleware, passes control to the next middleware in line.

18. To serve static files in Express, which built-in middleware would you use?

A. express.static()
B. express.file()
C. express.serve()
D. express.folder()

Answer:

A. express.static()

Explanation:

The express.static() middleware is used to serve static files in an Express application.

19. What does app.all() method do in Express.js?

A. It handles all HTTP methods.
B. It handles only POST and GET methods.
C. It restarts the app.
D. It closes all open routes.

Answer:

A. It handles all HTTP methods.

Explanation:

The app.all() method is a routing method to specify a callback function that gets executed when an endpoint (a combination of a request method and route) is hit.

20. Which function in Express.js wraps an asynchronous function to catch all the errors?

A. catchAsync()
B. asyncWrapper()
C. wrapAsync()
D. handleAsync()

Answer:

C. wrapAsync()

Explanation:

Though this isn't a built-in Express function, wrapAsync() is commonly used in the community to handle errors in asynchronous functions without needing to always use try-catch.

21. If you want to set several properties on a response at once in Express.js, which method would you use?

A. res.set()
B. res.properties()
C. res.headerSet()
D. res.multiSet()

Answer:

A. res.set()

Explanation:

The res.set() method is used in Express.js to set multiple headers in the response object.

22. How can you access the query string parameters in Express.js?

A. req.params
B. req.data
C. req.body
D. req.query

Answer:

D. req.query

Explanation:

The req.query object contains the query string parameters in Express.js.

23. What is the primary purpose of the Express 'Router'?

A. To route different files in the file system.
B. To divide the application into modular, mountable route handlers.
C. To connect to different databases.
D. To parse incoming requests.

Answer:

B. To divide the application into modular, mountable route handlers.

Explanation:

Express 'Router' is used to create modular, mountable route handlers in separate file(s) or modules.

24. How do you get the HTTP method (like GET, POST) of a client's request in Express?

A. req.methodType()
B. req.httpMethod
C. req.type()
D. req.method

Answer:

D. req.method

Explanation:

The req.method property contains a string representing the HTTP method of the request: GET, POST, PUT, and so on.

25. How do you retrieve the value of a specific cookie sent in a request in Express.js?

A. req.cookie.value
B. req.cookies[cookieName]
C. req.get('cookieName')
D. req.values.cookieName

Answer:

B. req.cookies[cookieName]

Explanation:

To retrieve the value of a specific cookie, you use req.cookies[cookieName] where cookieName is the name of the cookie you want to access.


Comments