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!

Q31

Q31 An application crashes with "Error: ENOENT, no such file or directory".
What should you check?

A

Disk space

B

File path

C

Node.js version

D

Network connection

Q32

Q32 What is the role of the event loop in Node.js?

A

To execute synchronous code

B

To manage I/O operations

C

To enhance CSS

D

To handle database queries

Q33

Q33 Which phase of the event loop executes timers scheduled by setTimeout and setInterval?

A

Poll phase

B

Check phase

C

Timers phase

D

Close callbacks phase

Q34

Q34 What happens during the 'poll' phase of the event loop?

A

Execution of callbacks

B

Processing of events

C

Execution of expired timers

D

Execution of I/O callbacks

Q35

Q35 Which phase of the event loop is responsible for executing callbacks from setImmediate()?

A

Poll phase

B

Check phase

C

Timers phase

D

Immediate phase

Q36

Q36 What is the primary benefit of Node.js's event-driven architecture?

A

Improved security

B

Ease of use

C

Scalability

D

Compatibility with other languages

Q37

Q37 Which method is used to defer the execution of a function until the next iteration of the event loop?

A

setTimeout

B

setInterval

C

setImmediate

D

nextTick

Q38

Q38 What is the output of the following code:
console.log('1'); process.nextTick(() => console.log('2')); console.log('3');

A

1 2 3

B

1 3 2

C

2 1 3

D

3 2 1

Q39

Q39 What is the output of the following code:
setTimeout(() => console.log('1'), 0); setImmediate(() => console.log('2'));

A

1 2

B

2 1

C

1

D

2

Q40

Q40 How can you monitor the phases of the event loop in a Node.js application?

A

Using console.log()

B

Using a debugger

C

Using process.nextTick()

D

Using event listeners

Q41

Q41 An application experiences high CPU usage and slow performance.
Which phase of the event loop might be causing this issue?

A

Poll phase

B

Timers phase

C

Close callbacks phase

D

Check phase

Q42

Q42 What is a stream in Node.js?

A

A way to handle I/O operations

B

A method for handling HTTP requests

C

A library for file manipulation

D

A type of database

Q43

Q43 Which of the following is a type of stream in Node.js?

A

Readable

B

Writable

C

Duplex

D

All of these

Q44

Q44 What is a buffer in Node.js?

A

A temporary storage for data

B

A method to read files

C

A HTTP handler

D

A type of stream

Q45

Q45 How do you create a buffer of 10 bytes in Node.js?

A

Buffer.alloc(10)

B

Buffer.create(10)

C

new Buffer(10)

D

Buffer.init(10)

Q46

Q46 What is the difference between Buffer.alloc and Buffer.allocUnsafe?

A

Buffer.alloc initializes the buffer with zeros

B

Buffer.allocUnsafe initializes the buffer with zeros

C

Buffer.alloc is deprecated

D

Buffer.allocUnsafe is deprecated

Q47

Q47 How do you create a readable stream in Node.js?

A

new stream.Readable()

B

fs.createReadStream()

C

stream.createRead()

D

new stream.Read()

Q48

Q48 How can you handle data events in a readable stream?

A

stream.on('data', callback)

B

stream.read(callback)

C

stream.write('data', callback)

D

stream.handle('data', callback)

Q49

Q49 What is the output of the following code:
const buf = Buffer.from('hello'); console.log(buf.toString('hex'));

A

68656c6c6f

B

hello

C

68656C6C6F

D

5a5a5a5a5a

Q50

Q50 What should you check if a readable stream is not emitting data?

A

Stream state

B

File permissions

C

Network connection

D

Buffer size

Q51

Q51 How can you handle errors in a stream?

A

Using a try-catch block

B

Using the error event

C

Restarting the stream

D

Ignoring the errors

Q52

Q52 What is the primary purpose of the HTTP module in Node.js?

A

To handle HTTP requests and responses

B

To handle file operations

C

To manage streams

D

To manage buffers

Q53

Q53 Which method is used to create a server in Node.js using the HTTP module?

A

http.createServer()

B

http.initServer()

C

http.newServer()

D

http.startServer()

Q54

Q54 Which event is emitted when the HTTP server receives a request?

A

request

B

data

C

connection

D

receive

Q55

Q55 What does the response.end() method do in the HTTP module?

A

Sends headers

B

Ends the response

C

Sends data

D

Closes the server

Q56

Q56 How can you set the response HTTP status code in Node.js?

A

response.setStatus()

B

response.statusCode()

C

response.writeHead()

D

response.sendStatus()

Q57

Q57 How do you write "Hello, World!" to the response in Node.js?

A

response.send('Hello, World!')

B

response.write('Hello, World!')

C

response.end('Hello, World!')

D

response.print('Hello, World!')

Q58

Q58 How do you read the request headers in Node.js?

A

request.headers

B

request.getHeaders()

C

request.readHeaders()

D

request.httpHeaders()

Q59

Q59 What is the output of the following code snippet?
const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('OK'); });

A

Content-Type

B

200 OK

C

plain text

D

OK

Q60

Q60 What should you check if an HTTP server in Node.js is not responding?

A

Server status

B

Network connection

C

Code syntax

D

HTTP version

ad verticalad vertical
ad