Learn Node Js MCQs

Prepare Node Js MCQs (Multiple choice Questions) for exam and job interviews.

Which module is used to create a server in Node.js?

1) http

2) server

3) network

4) express

Answer : Option 1

What is the purpose of the `process.on('exit', callback)` method?

1) To execute a function when the process exits

2) To start a new process

3) To handle errors

4) To manage HTTP requests

Answer : Option 1

How do you define custom error handling middleware in Express.js?

1) Using `app.use((err, req, res, next) => { ... })`

2) Using `app.error()`

3) Using `app.catch()`

4) Using `app.handleError()`

Answer : Option 1

What does the `os.homedir()` method return?

1) The home directory of the current user

2) The current working directory

3) The root directory

4) The system directory

Answer : Option 1

How do you retrieve data from a database in Node.js?

1) Using database clients and modules like `mysql` or `mongodb`

2) Using `fs` module

3) Using `http` module

4) Using `path` module

Answer : Option 1

What does the `res.end()` method do in Express.js?

1) Ends the response

2) Sends the response body

3) Sets the response headers

4) Redirects the client

Answer : Option 1

Which Node.js function is used to convert a string to JSON?

1) JSON.parse()

2) parse.json()

3) JSON.decode()

4) json.parse()

Answer : Option 1

How do you implement logging in a Node.js application?

1) Using libraries like `winston` or `morgan`

2) Using `console.log()`

3) Using `fs.writeFile()`

4) Using `debug` module

Answer : Option 1

What is the role of the `async` and `await` keywords?

1) To handle asynchronous code more cleanly

2) To execute synchronous code

3) To manage file systems

4) To parse JSON data

Answer : Option 1

How do you manage sessions in Express.js?

1) Using session middleware like `express-session`

2) Using `cookie-parser`

3) Using `body-parser`

4) Using `morgan`

Answer : Option 1