Search Tutorials


Express.js Quiz - MCQ - Multiple Choice Questions | JavaInUse

Express.js Quiz - MCQ - Multiple Choice Questions

Q. What is Express.js?

A. A front-end JavaScript framework
B. A back-end Node.js web framework
C. A mobile app development toolkit
D. A database management system

Q. How do you install Express.js in a Node.js project?

A. npm install express
B. yarn add express
C. Both A and B
D. None of the above

Q. What is the purpose of middleware in Express.js?

A. To handle requests and responses
B. To manage routing and URL mapping
C. To provide additional functionality and processing
D. All of the above

Q. How do you define a route in Express.js?

A. app.get('/route', function(req, res) {
    // route handling logic
});
B. app.route('/route').get(function(req, res) {
    // route handling logic
});
C. app.use('/route', function(req, res) {
    // route handling logic
});
D. app.set('/route', function(req, res) {
    // route handling logic
});

Q. What is the purpose of the res.send() method in Express.js?

A. To send a response back to the client
B. To set the HTTP status code
C. To set headers in the response
D. All of the above

Q. How can you access form data in a POST request using Express.js?

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

Q. What is the purpose of the next() function in Express.js middleware?

A. To skip the current middleware function
B. To pass control to the next middleware function
C. To end the request-response cycle
D. To send a response to the client

Q. How can you render a view in Express.js using EJS?

A. res.render('viewName', { data: 'value' });
B. res.view('viewName', { data: 'value' });
C. res.template('viewName', { data: 'value' });
D. res.ejs('viewName', { data: 'value' });

Q. How can you set a custom port for your Express.js application?

A. app.set('port', 3000);
B. app.use(3000);
C. app.listen(3000);
D. app.port = 3000;

Q. How can you handle errors in Express.js middleware?

A. Using the next(error) syntax
B. Throwing an Error object
C. Using the res.error() method
D. All of the above





Q. What is the purpose of the app.use() method in Express.js?

A. To define a route
B. To set a custom port
C. To use a middleware function
D. To render a view

Q. How can you access query parameters in a URL using Express.js?

A. req.query
B. req.params
C. req.queryParams
D. req.urlParams

Q. What is the purpose of the app.set() method in Express.js?

A. To set configuration settings
B. To define a route
C. To set the view engine
D. To set the HTTP method

Q. How can you redirect a user to a different URL in Express.js?

A. res.redirect('url');
B. res.location('url');
C. res.move('url');
D. res.change('url');

Q. What is the purpose of the app.get() method in Express.js?

A. To define a GET route
B. To get data from the request body
C. To get query parameters from the URL
D. To get the HTTP headers

Q. Which of the following code snippets correctly sets up a basic Express.js server and listens on a specified port?

A.
const express = require('express');
const app = express();

app.use(express.json());

const port = 3000;
app.listen(port, () => {
  console.log(<Server started on port> + port);
});
B.
const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));

const port = 3001;
app.listen(port, 'localhost', () => {
  console.log(<Server running on http://localhost:> + port);
});
C.
const express = require('express');

const app = express();

app.use(express.json(), express.urlencoded());

app.listen(3000, () => {
  console.log(<Express server listening on port 3000>);
});
D.
const express = require('express');
const app = express();

app.use(express.json(), express.urlencoded({ extended: false }));

app.listen(3001, () => {
  console.log(<Server started on http://localhost:3001>);
});

Q. How can you create a route in Express.js to handle a GET request to the /users endpoint and return a JSON response with an array of user objects?

A.
app.get('/users', (req, res) => {
  const users = [{ id: 1, name: 'John' }, { id: 2, name: 'Alice' }];
  res.json(users);
});
B.
app.route('/users')
  .get((req, res) => {
    const users = getUsers(); // Assume getUsers() returns an array of users
    res.json(users);
  });
C.
app.get('users', (req, res) => {
  db.collection('users').find().toArray((err, users) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.json(users);
    }
  });
});
D.
app.get('/get-users', (req, res) => {
  const users = getUserList(); // Assume getUserList() returns user list
  res.status(200).json(users);
});

Q. What will be the output of the following code snippet when a request is made to the /uppercase endpoint with the query parameter text=hello?

app.get('/uppercase', (req, res) => {
  const text = req.query.text.toUpperCase();
  res.send(text);
});
A.
HELLO
B.
hello
C.
Error: text parameter not found
D.
HELLO WORLD

Q. How can you create a route in Express.js to handle a POST request to the /register endpoint and extract the name and email from the request body?

A.
app.post('/register', (req, res) => {
  const { name, email } = req.body;
  // ... rest of the code
});
B.
app.route('/register')
  .post((req, res) => {
    const name = req.body.name;
    const email = req.body.email;
    // ... rest of the code
  });
C.
app.post('/register', (req, res, next) => {
  req.name = req.body.name;
  req.email = req.body.email;
  next();
});
D.
app.post('/register', (req, res) => {
  const name = req.params.name;
  const email = req.params.email;
  // ... rest of the code
});

Q. What will be the output of the following code snippet when a request is made to the /profile endpoint with the path parameter userId=123?

app.param('userId', (req, res, next, id) => {
  req.user = { id: id, name: 'John Doe' };
  next();
});

app.get('/profile/:userId', (req, res) => {
  res.send(req.user);
});
A.
{ id: 123, name: 'John Doe' }
B.
{ userId: 123, name: undefined }
C.
Error: userId parameter not found
D.
{ id: 123 }

Q. How can you create a route in Express.js to handle a PUT request to the /update-user/:userId endpoint and update the user's name in the request body?

A.
app.put('/update-user/:userId', (req, res) => {
  const { name } = req.body;
  const userId = req.params.userId;
  // Update the user with the new name and userId
});
B.
app.route('/update-user/:userId')
  .put((req, res) => {
    const name = req.body.name;
    const id = req.params.userId;
    // Update the user with the new name and id
  });
C.
app.put('/update-user', (req, res) => {
  const name = req.body.name;
  const userId = req.body.userId;
  // Update the user with the new name and userId
});
D.
app.put('/update-user/:id', (req, res) => {
  const name = req.body.name;
  const id = req.params.id;
  // Update the user with the new name and id
});

Q. What will be the output of the following code snippet when a request is made to the /calculate endpoint with the query parameters num1=5 and num2=10?

app.get('/calculate', (req, res) => {
  const num1 = parseInt(req.query.num1);
  const num2 = parseInt(req.query.num2);
  const sum = num1 + num2;
  res.send({ sum: sum });
});
A.
{ sum: 15 }
B.
{ sum: '510' }
C.
Error: num1 or num2 is not a number
D.
{ sum: NaN }

Q. How can you create a route in Express.js to handle a DELETE request to the /delete-user/:userId endpoint and delete the user with the specified ID?

A.
app.delete('/delete-user/:userId', (req, res) => {
  const userId = req.params.userId;
  // Delete the user with the specified userId
});
B.
app.route('/delete-user/:id')
  .delete((req, res) => {
    const id = req.params.id;
    // Delete the user with the specified id
  });
C.
app.delete('/:userId', (req, res) => {
  const userId = req.params.userId;
  // Delete the user with the specified userId
});
D.
app.delete('/delete/:id', (req, res) => {
  const id = req.params.id;
  // Delete the resource with the specified id
});

Q. What will be the output of the following code snippet when a request is made to the /repeat endpoint with the query parameter text=hello×=3?

app.get('/repeat', (req, res) => {
  const text = req.query.text;
  const repeatTimes = req.query['times'];
  const repeatedText = text.repeat(repeatTimes);
  res.send(repeatedText);
});
A.
hellohellohello
B.
hellotimes=3
C.
hello hello hello
D.
Error: times parameter not found