Search Tutorials


Node JS MCQ Questions and Answers | JavaInUse

Node JS MCQ Questions and Answers

Q. What is Node.js used for?

A. Server-side scripting
B. Client-side web development
C. Mobile app development
D. All of the above

Q. Which company developed Node.js?

A. Google
B. Microsoft
C. Mozilla
D. Joyent

Q. What is an advantage of using Node.js?

A. Single-threaded architecture
B. Cross-platform compatibility
C. Fast and scalable
D. All of the above

Q. Which of the following is a core module in Node.js?

A. http
B. fs
C. path
D. All of the above

Q. What is the purpose of the Node Package Manager (npm)?

A. To manage Node.js packages and dependencies
B. To run Node.js applications
C. To debug Node.js code
D. To deploy Node.js applications to the cloud

Q. What is an event-driven architecture?

A. An architecture where the flow of the program is determined by events
B. An architecture that uses callback functions to handle asynchronous operations
C. An architecture that uses promises to handle asynchronous code
D. All of the above

Q. What is the purpose of the "process" object in Node.js?

A. To provide information about the current Node.js process
B. To handle uncaught exceptions
C. To exit the Node.js process
D. All of the above

Q. What is a callback function in Node.js?

A. A function passed as an argument to another function
B. A function that is called synchronously
C. A function that is called asynchronously
D. A function that returns a value

Q. How can you check if a value is undefined in Node.js?

A. value == undefined
B. value === undefined
C. typeof value === "undefined"
D. All of the above

Q. What is the purpose of the "util" module in Node.js?

A. To provide utility functions for working with data structures
B. To format and inspect objects
C. To perform asynchronous operations
D. All of the above





Q. How can you create a new module in Node.js?

A. By creating a new .js file
B. By using the require() function
C. By using the module.exports object
D. All of the above

Q. What is the purpose of the "module.exports" object in Node.js?

A. To export functions, objects, or values from a module
B. To import functions from another module
C. To create a new module
D. To require a module

Q. What is the purpose of the "require()" function in Node.js?

A. To import a module
B. To export a module
C. To create a new module
D. To check if a module exists

Q. What is the event loop in Node.js?

A. A mechanism that allows Node.js to handle multiple concurrent connections
B. A way to execute asynchronous code
C. A loop that runs indefinitely in the background
D. All of the above

Q. What is the purpose of the "Buffer" class in Node.js?

A. To handle binary data
B. To store large amounts of data
C. To perform encryption and decryption
D. To handle JSON data

Which of the following code snippets demonstrates the correct way to create a basic HTTP server in Node.js using the HTTP module?

A.
const http = require('http');

http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, World!');
}).listen(8080);

console.log('Server running on http://localhost:8080');
B.
const http = require('http');

function handleRequest(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello, World!');
}

http.createServer(handleRequest).listen(8080);

console.log('Server running on http://localhost:8888');
C.
const http = require('http');

http.createServer(function(req, res) {
  res.write('Hello, World!');
  res.close();
}).listen(8080);

console.log('Server running on http://localhost:9090');
D.
const http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'application/json'});
  response.end(JSON.stringify({message: 'Hello, World!'}));
}).listen(8080);

console.log('Server running on http://localhost:8080');

Q17. Which of the following code snippets demonstrates the correct way to import and use the "fs" module in Node.js to read a file synchronously?

A.
const fs = require('fs');

let data = fs.readFileSync('file.txt');
console.log(data);
B.
const fs = require('fs-extra');

let data = fs.readFileSync('file.txt');
console.log(data);
C.
const fsModule = require('fs');

let fileData = fsModule.readFileSync('file.txt');
console.log(fileData);
D.
const fs = require('fs');

fs.readFile('file.txt', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Q18. Which of the following code snippets demonstrates the correct way to handle uncaught exceptions in a Node.js application?

A.
process.on('uncaughtException', (err) => {
  console.error('Uncaught Exception: ', err);
});
B.
process.on('uncaughtError', (err) => {
  console.error('Uncaught Error: ', err);
});
C.
process.on('uncaughtException', function(err) {
  console.error('Error:', err.message);
});
D.
process.on('uncaughtException', (err) => {
  console.error('Error:', err.stack);
});

Q19. Which of the following code snippets demonstrates the correct way to use the "path" module in Node.js to join two path segments?

A.
const path = require('path');

let filePath = path.join('/path/to/directory', 'file.txt');
console.log(filePath);
B.
const pathModule = require('path');

let fullPath = pathModule.join('/path', 'to', 'file.txt');
console.log(fullPath);
C.
const path = require('path');

let fullPath = path.combine('/path', 'to', 'file.txt');
console.log(fullPath);
D.
const path = require('path');

let filePath = path.concat('/path/to', 'file.txt');
console.log(filePath);

Q20. Which of the following code snippets demonstrates the correct way to create a middleware function in Express.js that will be executed for every request?

A.
function myMiddleware(req, res, next) {
  console.log('Middleware executed!');
  next();
}

app.use(myMiddleware);
B.
app.use(function(req, res, next) {
  console.log('Middleware executed!');
  next();
});
C.
const myMiddleware = (req, res, next) => {
  console.log('Middleware executed!');
  next();
};

app.use(myMiddleware);
D.
function myMiddleware(req, res) {
  console.log('Middleware executed!');
  res.end();
}

app.use(myMiddleware);

Q21. Which of the following code snippets demonstrates the correct way to send a JSON response in an Express.js route handler?

A.
app.get('/data', (req, res) => {
  res.json({message: 'Hello, World!'});
});
B.
app.get('/data', function(req, res) {
  res.send({message: 'Hello, World!'});
});
C.
app.get('/data', (req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({message: 'Hello, World!'}));
});
D.
app.get('/data', (req, res) => {
  res.contentType('json');
  res.send({message: 'Hello, World!'});
});

Q22. Which of the following code snippets demonstrates the correct way to use the "crypto" module in Node.js to generate a hash of a password?

A.
const crypto = require('crypto');

let hash = crypto.createHash('sha256').update('password').digest('hex');
console.log(hash);
B.
const crypto = require('crypto');

let hash = crypto.createHash('md5').update('password').digest('base64');
console.log(hash);
C.
const crypto = require('crypto');

let hash = crypto.createHash('sha1').update('password').digest('hex');
console.log(hash);
D.
const crypto = require('crypto');

let hash = crypto.createHash('sha256').update('password').digest('base64');
console.log(hash);

Q23. Which of the following code snippets demonstrates the correct way to use the "events" module in Node.js to create and emit a custom event?

A.
const events = require('events');

const eventEmitter = new events.EventEmitter();

eventEmitter.on('myEvent', () => {
  console.log('Event emitted!');
});

eventEmitter.emit('myEvent');
B.
const events = require('events');

class MyEmitter extends events.EventEmitter {
  constructor() {
    super();
  }

  emitEvent() {
    this.emit('myEvent');
  }
}

const myEmitter = new MyEmitter();
myEmitter.on('myEvent', () => {
  console.log('Event emitted!');
});

myEmitter.emitEvent();
C.
const events = require('events');

const eventEmitter = new events();

eventEmitter.addListener('myEvent', () => {
  console.log('Event emitted!');
});

eventEmitter.trigger('myEvent');
D.
const events = require('events');

const eventEmitter = new events.EventEmitter();

eventEmitter.addEventListener('myEvent', () => {
  console.log('Event emitted!');
});

eventEmitter.fire('myEvent');