node js banner

Node JS Multiple Choice Questions (MCQs) and Answers

Master Node JS with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of Node JS concepts. Begin your placement preparation journey now!

Q91

Q91 How do you handle errors in a REST API using Express.js?

A

app.use((err, req, res, next) => { res.status(500).json({ error: err.message }); })

B

app.use((req, res, next) => { res.status(500).json({ error: 'Server error' }); })

C

app.use((req, res, next) => { res.json({ error: 'Server error' }); })

D

app.use((err, req, res, next) => { res.send({ error: 'Server error' }); })

Q92

Q92 What should you check if a REST API is not returning the expected data?

A

API endpoint

B

HTTP method

C

Request payload

D

Response format

Q93

Q93 How can you debug a REST API request in Node.js?

A

Using console.log()

B

Using a debugger

C

Using Postman

D

Checking network tab in browser

Q94

Q94 What is a common cause of CORS errors in REST APIs?

A

Incorrect URL

B

Invalid JSON

C

Cross-origin requests

D

Server overload

Q95

Q95 Which module is commonly used for MongoDB integration in Node.js?

A

mongoose

B

mysql

C

sequelize

D

pg

Q96

Q96 What does the mongoose.connect() method do?

A

Closes the database connection

B

Opens a new database connection

C

Runs a database query

D

Initializes the Mongoose library

Q97

Q97 Which method is used to save a document to a MongoDB collection in Mongoose?

A

save

B

insert

C

create

D

update

Q98

Q98 What is a schema in Mongoose?

A

A function to run database queries

B

A middleware function

C

A blueprint for defining the structure of a document

D

A method for connecting to the database

Q99

Q99 What is the purpose of model validation in Mongoose?

A

To validate JavaScript code

B

To ensure data integrity

C

To check database connection

D

To manage middleware

Q100

Q100 How do you define a schema in Mongoose?

A

new mongoose.Schema()

B

mongoose.createSchema()

C

mongoose.initSchema()

D

new Schema()

Q101

Q101 How do you save a new document to a MongoDB collection using Mongoose?

A

const doc = new Model(data); doc.save();

B

Model.insert(data);

C

Model.create(data);

D

const doc = new Model(data); doc.insert();

Q102

Q102 How do you update an existing document in Mongoose?

A

Model.update(query, update)

B

Model.updateOne(query, update)

C

Model.modify(query, update)

D

Model.change(query, update)

Q103

Q103 What should you check if a MongoDB connection fails in Mongoose?

A

Connection string

B

Model definition

C

Schema validation

D

Middleware

Q104

Q104 How can you debug database queries in Mongoose?

A

Using a debugger

B

Using mongoose.set('debug', true)

C

Checking model definitions

D

Checking schema definitions

Q105

Q105 What is the primary purpose of JWT (JSON Web Token) in authentication?

A

To store session data

B

To encrypt passwords

C

To transmit information securely

D

To manage user roles

Q106

Q106 Which of the following is a common strategy for implementing authentication in Express.js applications?

A

OAuth

B

JWT

C

Session-based

D

Cookie-based

Q107

Q107 What is the main advantage of using OAuth for authentication?

A

Simplicity

B

Improved security

C

Speed

D

Ease of use

Q108

Q108 What is the purpose of the Passport.js library in a Node.js application?

A

To manage database connections

B

To handle routing

C

To authenticate requests

D

To parse JSON bodies

Q109

Q109 How do you initialize Passport.js in an Express.js application?

A

app.use(passport.init());

B

app.use(passport.start());

C

app.use(passport.initialize());

D

app.use(passport.begin());

Q110

Q110 How do you protect a route using JWT in Express.js?

A

app.use(jwtAuth);

B

app.get('/protected', jwtAuth, (req, res) => {...});

C

app.use(jwtAuth());

D

app.get('/protected', (req, res) => { jwtAuth(); ... });

Q111

Q111 How do you use Passport.js to authenticate users with Google OAuth?

A

passport.use(new GoogleStrategy(...));

B

passport.use(new OAuthStrategy(...));

C

passport.use(new GoogleAuth(...));

D

passport.use(new AuthStrategy(...));

Q112

Q112 What should you check if JWT authentication fails in your application?

A

Token expiry

B

Token signature

C

User permissions

D

Token payload

Q113

Q113 How can you debug issues with Passport.js authentication?

A

Check strategy configuration

B

Check middleware order

C

Check session settings

D

Check callback URL

Q114

Q114 What is the primary purpose of error handling in Node.js?

A

To manage database connections

B

To handle HTTP requests

C

To catch and manage runtime errors

D

To improve performance

Q115

Q115 Which module is commonly used for logging errors in a Node.js application?

A

fs

B

path

C

winston

D

http

Q116

Q116 What is the purpose of a try-catch block in JavaScript?

A

To handle asynchronous operations

B

To catch and handle exceptions

C

To manage HTTP requests

D

To handle database queries

Q117

Q117 How do you define a custom error in Node.js?

A

throw new Error('message')

B

console.error('message')

C

process.exit(1)

D

return new Error('message')

Q118

Q118 How do you handle errors in an Express.js middleware function?

A

Using try-catch blocks

B

Using if-else statements

C

Using error-handling middleware

D

Using callbacks

Q119

Q119 How do you propagate an error in a callback function in Node.js?

A

return error

B

throw error

C

next(error)

D

process.exit(1)

Q120

Q120 How do you handle uncaught exceptions in a Node.js application?

A

Using try-catch blocks

B

Using process.on('uncaughtException', callback)

C

Using a promise

D

Using setTimeout

ad verticalad vertical
ad