Learn Node Js MCQs

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

How do you use the `child_process` module to spawn a new process?

1) By using `child_process.exec()`

2) By using `child_process.fork()`

3) By using `child_process.execFile()`

4) By using `child_process.spawn()`

Answer : Option 4

What is the role of the `crypto` module in Node.js?

1) To handle HTTP requests

2) To manage file systems

3) To parse URLs

4) To provide cryptographic functionality

Answer : Option 4

How do you perform URL encoding in Node.js?

1) Using `decodeURIComponent()`

2) Using `encodeURI()`

3) Using `decodeURI()`

4) Using `encodeURIComponent()`

Answer : Option 4

What does the `path.normalize()` method do?

1) Joins multiple path segments

2) Extracts the directory name

3) Returns the file extension

4) Normalizes a path by resolving `..` and `.` segments

Answer : Option 4

How do you use the `assert` module for unit testing?

1) By using `console.log()`

2) By using `debug` module

3) By using `fs` module

4) By using assertion methods to validate test conditions

Answer : Option 4

What is the purpose of the `process.hrtime()` method?

1) To get the current system time

2) To measure elapsed time

3) To handle performance profiling

4) To measure high-resolution time intervals

Answer : Option 4

How do you use the `fs` module to watch for file changes?

1) Using `fs.createReadStream()`

2) Using `fs.createWriteStream()`

3) Using `fs.readFile()`

4) Using `fs.watch()` method

Answer : Option 4

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

1) The system CPU usage

2) The system memory usage

3) The system uptime

4) The system load averages for the last 1, 5, and 15 minutes

Answer : Option 4

How do you handle HTTP headers in Express.js?

1) Using `req.headers` property

2) Using `res.send()` method

3) Using `res.json()`

4) Using `res.set()` or `res.header()` methods

Answer : Option 4

What is the purpose of the `util.promisify()` function?

1) To handle asynchronous code

2) To manage file systems

3) To parse URLs

4) To convert callback-based functions into Promise-based functions

Answer : Option 4