/nodejs-questions

Some common node.js questions and answers.

NodeJS Questions

Some common node.js questions and answers.

1. Which one is the node global object?
  • A: log
  • B: import
  • C: process
  • D: http
Answer

Answer: C

process is one of the node global object. Global objects are globally accessible. These are, **dirname, **filename, module, process, setTimeout, setInterval, setImmediate.

2. Which module is used to provide compression and decompression functionalities?
  • A: net
  • B: buffer
  • C: zlib
  • D: dns
Answer

Answer: C

zlib module provides compression(zip) and decompression(unzip) functionalities.

3. Which console method is used to show the stack trace?
  • A: stack
  • B: log
  • C: trace
  • D: debug
Answer

Answer: C

trace method is used to show the stack trace.

4. Which module would you use to get the server information?
  • A: dns
  • B: url
  • C: net
  • D: tls
Answer

Answer: A

dns module is used to get the server information.

5. Which one is used to read a file asynchronously?
  • A: fs.getFile
  • B: fs.read
  • C: fs.readFile
  • D: fs.get
Answer

Answer: C

fs.readFile is used to read a file asynchronously.

6. Which module would you use to encrypt data?
  • A: crypto
  • B: process
  • C: encrypt
Answer

Answer: A

crypto module is used to encrypt data.

7. Which method is used to exit(kill) in node.js?
  • A: kill
  • B: exit
  • C: stop
Answer

Answer: B

process.exit() is used to exit in node.js.

8. Which one is the core module of node?
  • A: require
  • B: import
  • C: fs
  • D: process
Answer

Answer: C

fs is a core module of node. Some of them are, buffer, crypto, dns etc.

9. Which command is used to tell node to run javascript strict mode in node repl?
  • A: node -strict
  • B: node --strict
  • C: node --use-strict
  • D: node -use--strict
Answer

Answer: C

node --use-strict will run node in javascript strict mode.

10. Which one is not a node repl command?
  • A: .save
  • B: .clear
  • C: .help
  • D: .find
Answer

Answer: D

.find is not a node repl command. The repl commands are, .break, .clear, .editor, .exit, .help, .load, .save.

11. Which one is an instance of Event Emitter?
  • A: process
  • B: fs
  • C: http
  • D: require
Answer

Answer: A

All objects that emit events are instances of the EventEmitter class. process emit events, so it is an instance of Event Emitter. You can check this via process instanceof require('events').EventEmitter.

12. How can you get the file name(with the directory) that you are working on?
  • A: console.log(__dirname)
  • B: console.log(filename)
  • C: console.log(__filename)
  • D: console.log(filename)
Answer

Answer: C

console.log(__filename) will show the file name.

13. How can you check if a module 'x' exist locally?
  • A: require.get(x)
  • B: require.resolve(x)
  • C: require.find(x)
Answer

Answer: B

require.resolve(x) can give you the full path of the module.

14. Which one is not a correct property of process(global object)?
  • A: .stdin
  • B: .title
  • C: .clear
  • D: .execPath
Answer

Answer: C

.clear is not a property of process.

15. How can you get absolute path for a specific file app.js?
  • A: __filename('app.js')
  • B: path.absolute('app.js')
  • C: path.resolve('app.js')
  • D: __dirname('app.js')
Answer

Answer: C

path.resolve('app.js') will return the absolute path for app.js.

16. Which method is not a correct method for OS module?
  • A: cpus()
  • B: platform()
  • C: type()
  • D: version()
Answer

Answer: D

version() is not a correct method for os module.

17: Which one is a correct method to create child_process?
  • A: fork()
  • B: exec()
  • C: spawn()
  • D: all
Answer

Answer: D

all, there are three ways to create child_process, child_process.exec(), child_process.spawn() & child_process.fork().

18: Which DNS method returns an array of record types belonging to the specified hostname?
  • A: resolve()
  • B: lookupService()
  • C: getServers()
  • D: lookup()
Answer

Answer: A

resolve(), the resolve function returns an array of record types.

19: Which object holds arguments pass through a node command?
  • A: cli.arguments
  • B: process.arguments
  • C: process.args
  • D: process.argv
Answer

Answer: D

process.argv, holds arguments pass through a node command.

20: In node 12+, how can you make readFile a promise based method?
  • A: const { readFile } = require('fs').promises
  • B: const { readFile } = require('fs')
  • C: const { readFile } = require('promises')
Answer

Answer: A

const { readFile } = require('fs').promises, by using this you can make readFile a promise based method.

readFile("./file.txt", { encoding: "utf8" })
  .then((data) => console.log(data))
  .catch((error) => console.error(error));
21: Which one provides Event Loop?
  • A: V8
  • B: Libuv
  • C: C++ bindings
Answer

Answer: B

Libuv library provides event loop.

22: APIs of Node.JS are
  • A: Asynchronous
  • B: Synchronous
  • C: Both of the above
  • D: None of the above
Answer

Answer: C

APIs of Node.JS are Asynchronous and Synchronous.

23: Which module is used to decode Buffer object into strings?
  • A: decoder
  • B: buffer
  • C: string_buffer
  • D: string_decoder
Answer

Answer: D

string_decoder is used to decode Buffer object into strings.

24: Which one is used to expose the node.js modules?
  • A: module
  • B: module.expose
  • C: exports
  • D: module.exports
Answer

Answer: D

Example:

module.exports.bar = "bar";
25: To execute the code of index.js which method should use?
  • A: npm index.js
  • B: node.index.js
  • C: node index.js
  • D: npm run index.js
Answer

Answer: C

node filepath is used to run any file.

26: Which module is used to serve static resources in Node.js?
  • A: require
  • B: node-resource
  • C: fs
  • D: node-static
Answer

Answer: D

You can use node-static module to serve static resources. This module is an HTTP static-file server module with built-in caching.

27: How to install a package (my-package) in node js?
  • A: require my-package
  • B: node install my-package
  • C: npm install my-package
  • D: none of the above
Answer

Answer: C

To install any package in node js just run npm install package-name (available in npmjs.com). example: npm install my-package

28: Which one is used to create and consume custom events?
  • A: NodeEvent
  • B: EventEmitter
  • C: Both of them
  • D: none of the above
Answer

Answer: B

EventEmitter class lies in the events module. It can be accessible like this −

// Import events module
var events = require("events");
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
29: Valid form of route path in node.js?
  • A: Regular expressions
  • B: String
  • C: String patterns
  • D: All of them
Answer

Answer: D

You can use String, String pattern and regular expressions to form a route path.

30: Which module is used to create a server?
  • A: fs
  • B: http
  • C: server
  • D: none of the above
Answer

Answer: B

We use the http instance and call http.createServer() method to create a server instance example:

var http = require("http");
31: Input arguments for an asynchronous queue?
  • A: Task function
  • B: Concurrency value
  • C: Both of them
  • D: None of the above
Answer

Answer: C

Concurrency value and Task function are the main arguments that an asynchronous queue uses.

32: Which method is used to get the filename part of a file path?
  • A: path.basename
  • B: path.dirname
  • C: path.parse
  • D: path.join
Answer

Answer: A

path.basename is user to get the filename part of a file path.