Learn Node Js MCQs

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

How do you define and use global variables in Node.js?

1) Using `process.env`

2) Using `module.exports`

3) Using `window` object

4) Using the `global` object

Answer : Option 4

What is the purpose of the `console.time()` and `console.timeEnd()` methods?

1) To log errors

2) To manage file operations

3) To handle HTTP requests

4) To measure the time taken for operations

Answer : Option 4

How do you handle asynchronous code in Node.js?

1) Using synchronous functions

2) By using file system operations

3) By using HTTP requests

4) Using callbacks, Promises, or async/await

Answer : Option 4

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

1) The operating system platform

2) The CPU model

3) The system memory

4) The system’s hostname

Answer : Option 4

How do you create a writable stream in Node.js?

1) Using `fs.createReadStream()`

2) Using `http.createServer()`

3) Using `Buffer` class

4) Using `fs.createWriteStream()`

Answer : Option 4

What is the purpose of the `assert.deepEqual()` method?

1) To test shallow equality

2) To compare strings

3) To compare numbers

4) To test deep equality between objects

Answer : Option 4

How do you read JSON data from a file in Node.js?

1) By using `fs.readFileSync()` and `JSON.stringify()`

2) By using `fs.createReadStream()`

3) By using `http.get()`

4) By using `fs.readFile()` and `JSON.parse()`

Answer : Option 4

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

1) The free system memory

2) The used system memory

3) The system memory usage percentage

4) The total system memory

Answer : Option 4

How do you handle synchronous file operations in Node.js?

1) Using `fs.createReadStream()`

2) Using `fs.createWriteStream()`

3) Using `http` module

4) Using `fs.readFileSync()` and `fs.writeFileSync()`

Answer : Option 4

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

1) To handle HTTP requests

2) To manage file systems

3) To handle streams

4) To parse and format URLs

Answer : Option 4