Some common node.js questions and answers.
- 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