NodeJS Questions & Answers
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 javascript 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.
33: The $ npm ls statement is used to list down all the _?
- A: Web application
- B: nodejs sample modules
- C: locally installed modules
- D: package modules
Answer
Answer: C
The $ npm ls statement is used to list down all the locally installed modules.
34: Which amongst the following mehtod is used to create instance of http module?
- A:
var http = require('http');
- B:
var http = new require('http');
- C:
var http = new http();
- D: None of these
Answer
Answer: A
var http = require('http');
is the methods of create instance of http module.
Node.js
.
35: The __ core module is used to create a web server in - A:
fs
- B:
url
- C:
connect
- D:
http
Answer
Answer: D
The http
core module is used to create a web server in Node.js
36: Which of the following code gets length of a buffer buf?
- A: buf.length
- B: buf.size
- C: buf.length()
- D: buf.size()
Answer
Answer: A
buf.length
returns size of a node buffer in bytes.
37: Which function is used to include modules in Node Js.
- A: include()
- B: require()
- C: attach()
- D: all
Answer
Answer: B
require();
function is used to include modules in Node Js.
38: Which of the following is a GUI-based debugging tool for Node.js?
- A: Core node debugger
- B: Console
- C: REPL
- D: Node Inspector
Answer
Answer: D
Node Inspector is a debugger interface for Node.js
applications that uses the Blink Developer Tools (formerly WebKit Web Inspector).
39: Which command is used to silence all process warnings including deprecations for app.js?
- A: node app.js --trace-warnings
- B: node app.js --no-deprecation
- C: node app.js --no-warnings
- D: node app.js --trace-deprecation
Answer
Answer: C
node app.js --no-warnings
is used to silence all process warnings including deprecations for app.js.
40: Which method is used to convert path segments into string using the platform-specific separator as a delimiter?
- A: path.join()
- B: path.parse()
- C: path.normalize()
- D: path.format()
Answer
Answer: A
path.join()
method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
41: Which module is used to take advantage of multi-core systems?
- A: os
- B: dns
- C: stream
- D: cluster
Answer
Answer: D
cluster
is used to take advantage of multi-core systems. As node.js runs as single threaded we can take advantage of multi-core systems using cluster module.
42: Which of the following are Node.js streams types?
- A: writable
- B: transform
- C: readable
- D: all of the above
Answer
Answer: D
There are four main types of streamsin
Node. js:
readable,
writable,
duplexand
transform`. Each stream is an eventEmitter instance that emits different events at several intervals.
43: How to test if two nodes are equal?
- A: isEqualNode()
- B: equal()
- C: ==
- D: None of the above
Answer: A
The Node. isEqualNode()
method tests whether two nodes are equal.
44: Third-party packages can be install/update/delete using __.
- A: Node.exe
- B: Node Package Manager
- C: module.exports
- D: REPL
Answer: B
Third-party packages can be install/update/delete using NPM (Node Package Manager)
45: When you run JavaScript in a Node.js application, which element in a Node.js stack actually executes that JavaScript?
- A: the libuv library
- B: the VM (like V8 or Chakra)
- C: the c-ares library
- D: the repl module
Answer: B
The VM element will executes when run a Node.js applications.
46: What is the default scope in Node.js application?
- A: Global
- B: Global Function
- C: Local
- D: Local to object
Answer: C
Local is the default scope in the Node. js application.
47: Which method of fs module is used to close a file?
- A: fs.close(fd, callback)
- B: fs.closeFile(fd, callback)
- C: fs.closePath(fd, callback)
- D: None of the above.
Answer: A
fs.close(fd, callback) is the method which is used to close a file.
48: Which method of fs module is used to remove a directory?
- A: fs.deleteDirectory(path[, mode], callback)
- B: fs.rmdir(path, callback)
- C: fs.removeDirectory(path[, mode], callback)
- D: None of the above.
Answer: B
fs.rmdir(path, callback) is the method which is used to remove a directory.
49: Which of the following code gets length of a buffer buf?
- A: buf.length
- B: buf.size
- C: buf.length()
- D: buf.size()
Answer: A
buf.length returns size of a node buffer in bytes.
50: Which of the following platforms does Node.js support?
- A: Windows
- B: Macintosh
- C: Unix/Linux
- D: All of the above.
Answer: D
Node.js is supported on all of the above written operating systems.