Node.JS

Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. We have list down some important question and answers for fresher and professional to crack the job interviews.

Q.1 What do you understand by callback, in NodeJS?
Callback is a function that is to be executed after another function has finished executing  or called after a task is finished. It is asynchronous equivalent for a function. All APIs of Node are written is such a way that they supports callbacks.
Q.2 Describe Reactor Pattern in Node.js?

Reactor Pattern is an idea of non-blocking I/O operations in Node.js. This pattern provides a handler(in case of Node.js, a callback function) that is associated with each I/O operation. When an I/O request is generated, it is submitted to a demultiplexer. This demultiplexer is a notification interface that is used to handle concurrency in non-blocking I/O mode and collects every request in form of an event and queues each event in a queue.

Thus, the demultiplexer provides the Event Queue. At the same time, there is an Event Loop which iterates over the items in the Event Queue. Every event has a callback function associated with it, and that callback function is invoked when the Event Loop iterates.

Q.3 Differentiate between process.nextTick() and setImmediate()?
The difference between process.nextTick() and setImmediate() is that process.nextTick() defers the execution of an action till the next pass around the event loop or it simply calls the callback function once the ongoing execution of the event loop is finished whereas setImmediate() executes a callback on the next cycle of the event loop and it gives back to the event loop for executing any I/O operations.
Q.4 What is event loop in JavaScript?
Event loop, as the name suggests is loop which runs in background listening for various event emitters.
Q.5 What is a callback in Node.js?
Callback is a piece of code which is passed as an argument to another function (say fn1) to execute the callback function (say fn2) after executing the function fn1. This is used to handle the asynchronous behaviour of javascript. The functionality of a callback can be imagined as somewhat analogous to Interfaces in JAVA.
Q.6 What is callback hell and how can it be avoided?
Callback hell refers to a coding pattern where there is a lot of nesting of callback functions. The code forms a pyramid like structure and it becomes difficult to debug. Callback hell refers to a coding pattern where there is a lot of nesting of callback functions. The code forms a pyramid like structure and it becomes difficult to debug. It can be avoided by: a. Using promises b. Yield operator and Generator functions from ES6 c. Modularizing code d. Using async library (http://caolan.github.io/async/)
Q.7 What is a Promise in Node.Js?
A promise is a method that eventually produces a value. Instead of passing a function as argument as with callbacks, “Once the result is received from an asynchronous operation then the required function is executed”. The required functions are never passed as arguments to the asynchronous operation. It has the form: callAsyncFunction() .then(firstFunction) .then(secondFunction) .then(thirdFunction) .then(fourthFunction);
Q.8 What are ACID properties wrt to a database?
ACID stands for Atomicity, Consistency, Isolation and Durability. Atomicity It means that either the record/document updates completely or does not update with respect to the operation. Consistency It implies that a transaction either creates a new and valid state of data, or, if any failure occurs, returns all data to its state before the transaction was started. Isolation Two or more simultaneous database queries should run independent of the other transactions. Durability Data once committed should be saved in memory so that even in case of system failure, data is not affected.
Q.9 Where and why should you not use NodeJS?
NodeJS should NOT be used where computations are CPU intensive. Eg: Data Analytics Server, Image Processing Servers, Video Processing Servers etc. NodeJS is meant for highly I/O-bound operations which does not require heavy CPU intensive operation/tasks.
Q.10 What is the relation of Node.js with JavaScript?
Node.js is not a JavaScript framework, many of its modules are written in JavaScript. - It allows the developers to create new modules in JavaScript. - Node.js is a virtual machine that leverages JavaScript as its scripting language to achieve high output.
Q.11 Can you provide some good examples of config file separation for dev and prod environments.

A perfect and flawless configuration setup should ensure: - keys can be read from file AND from environment variable - secrets are kept outside committed code - config is hierarchical for easier findability Consider the following config file:


Q.12 What is the concept of error-first callback in NodeJS
The Node.js core API follow an pattern called error-first callback, in which a callback function is passed to the method with error object (error and data) is passed as first argument. If no error is encountered, then null is passed
Q.13 Where does global dependencies are located under NodeJS
During installation, global dependencies are located in the local node_modules folder or as defined by NODE_PATH environment variable
Q.14 Which command in NodeJS, will list installed global libraries
The command is - npm list -g to see where global libraries are installed.
Q.15 Which field value in package.json file under NodeJS, lists one or more executable files like to install into the PATH
bin, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
Get Govt. Certified Take Test