Some common node.js questions and answers which will help you to test some basic knowledge of node.js.
- A: log
- B: import
- C: process
- D: http
Answer
process
is one of the node global object. Global objects are globally accessible. These are, **dirname, **filename, module, process, setTimeout, setInterval, setImmediate.
- A: net
- B: buffer
- C: zlib
- D: dns
- A: stack
- B: log
- C: trace
- D: debug
- A: dns
- B: url
- C: net
- D: tls
- A: fs.getFile
- B: fs.read
- C: fs.readFile
- D: fs.get
- A: crypto
- B: process
- C: encrypt
- A: kill
- B: exit
- C: stop
- A: require
- B: import
- C: fs
- D: process
- A: node -strict
- B: node --strict
- C: node --use-strict
- D: node -use--strict
- A: .save
- B: .clear
- C: .help
- D: .find
Answer
.find
is not a node repl command. The repl commands are, .break, .clear, .editor, .exit, .help, .load, .save.
- A: process
- B: fs
- C: http
- D: require
Answer
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
.
- A: console.log(__dirname)
- B: console.log(filename)
- C: console.log(__filename)
- D: console.log(filename)
- A: require.get(x)
- B: require.resolve(x)
- C: require.find(x)
- A: .stdin
- B: .title
- C: .clear
- D: .execPath
- A: __filename('app.js')
- B: path.absolute('app.js')
- C: path.resolve('app.js')
- D: __dirname('app.js')
- A: cpus()
- B: platform()
- C: type()
- D: version()
- A: fork()
- B: exec()
- C: spawn()
- D: all
Answer
all
, there are three ways to create child_process, child_process.exec(), child_process.spawn() & child_process.fork().
- A: resolve()
- B: lookupService()
- C: getServers()
- D: lookup()
- A: cli.arguments
- B: process.arguments
- C: process.args
- D: process.argv
- A: const { readFile } = require('fs').promises
- B: const { readFile } = require('fs')
- C: const { readFile } = require('promises')
Answer
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));
- A: V8
- B: Libuv
- C: C++ bindings
- A: Asynchronous
- B: Synchronous
- C: Both of the above
- D: None of the above
- A: decoder
- B: buffer
- C: string_buffer
- D: string_decoder
- A: module
- B: module.expose
- C: exports
- D: module.exports
- A: npm index.js
- B: node.index.js
- C: node index.js
- D: npm run index.js
- A: require
- B: node-resource
- C: fs
- D: node-static
Answer
You can use node-static
module to serve static resources. This module is an HTTP static-file server module with built-in caching.
- A: require my-package
- B: node install my-package
- C: npm install my-package
- D: none of the above
Answer
To install any package in node js just run npm install package-name
(available in npmjs.com).
example: npm install my-package
- A: NodeEvent
- B: EventEmitter
- C: Both of them
- D: none of the above
Answer
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();
- A: Regular expressions
- B: String
- C: String patterns
- D: All of them
- A: fs
- B: http
- C: server
- D: none of the above
Answer
We use the http instance and call http.createServer()
method to create a server instance
example:
var http = require("http");
- A: Task function
- B: Concurrency value
- C: Both of them
- D: None of the above
Answer
Concurrency value and Task function are the main arguments that an asynchronous queue uses.
- A: path.basename
- B: path.dirname
- C: path.parse
- D: path.join
- A: Web application
- B: nodejs sample modules
- C: locally installed modules
- D: package modules
- A:
var http = require('http');
- B:
var http = new require('http');
- C:
var http = new http();
- D: None of these
- A:
fs
- B:
url
- C:
connect
- D:
http
- A: buf.length
- B: buf.size
- C: buf.length()
- D: buf.size()
- A: include()
- B: require()
- C: attach()
- D: all
- A: Core node debugger
- B: Console
- C: REPL
- D: Node Inspector
Answer
Node Inspector is a debugger interface for Node.js
applications that uses the Blink Developer Tools (formerly WebKit Web Inspector).
- 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
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
path.join()
method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
- A: os
- B: dns
- C: stream
- D: cluster
Answer
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.
- A: writable
- B: transform
- C: readable
- D: all of the above
Answer
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.
- A: isEqualNode()
- B: equal()
- C: ==
- D: None of the above
- A: Node.exe
- B: Node Package Manager
- C: module.exports
- D: REPL
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
- A: Global
- B: Global Function
- C: Local
- D: Local to object
- A: fs.close(fd, callback)
- B: fs.closeFile(fd, callback)
- C: fs.closePath(fd, callback)
- D: None of the above.
- A: fs.deleteDirectory(path[, mode], callback)
- B: fs.rmdir(path, callback)
- C: fs.removeDirectory(path[, mode], callback)
- D: None of the above.
- A: buf.length
- B: buf.size
- C: buf.length()
- D: buf.size()
- A: Windows
- B: Macintosh
- C: Unix/Linux
- D: All of the above.