Showing posts with label Node.js Interview Questions. Show all posts
Showing posts with label Node.js Interview Questions. Show all posts

Sunday, October 21, 2018

Advance Node.JS Interview Questions And Answers

Now a days NodeJs is very popular in the industry. If you're looking for Node.JS Interview Questions for Experienced or Beginners, you are at right place. There are lot of opportunities from many reputed companies in the world. According to research Node.JS has a market share of about 2.6%. So, You still have opportunity to move ahead in your career in Node.JS Development. Techbowl arranged Advanced Node JS Interview Questions 2018 or node js programming interview questions that helps you in cracking your interview & acquire dream career as Node.JS Developer.

Advance Node.JS Interview Questions And Answers


Advance Node.JS Interview Questions And Answers



Q1: What is Node.js?

Node.js is a web application framework built on Google Chrome's JavaScript Engine (V8 Engine).

Node.js comes with runtime environment on which a Javascript based script can be interpreted and executed (It is analogus to JVM to JAVA byte code). This runtime allows to execute a JavaScript code on any machine outside a browser. Because of this runtime of Node.js, JavaScript is now can be executed on server as well.


Node.js = Runtime Environment + JavaScript Library

Q2: What is an error-first callback?

Error-first callbacks are used to pass errors and data. The first argument is always an error object that the programmer has to check if something went wrong. Additional arguments are used to pass data.

fs.readFile(filePath, function(err, data) {
  if (err) {
    //handle the error
  }
  // use the data object
});


Q3: What are the benefits of using Node.js?

Following are main benefits of using Node.js

  • Aynchronous and Event Driven- All APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based server never waits for a API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call.
  • Very Fast- Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but highly Scalable - Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.
  • No Buffering - Node.js applications never buffer any data. These applications simply output the data in chunks.

Q4: If Node.js is single threaded then how it handles concurrency?

Node provides a single thread to programmers so that code can be written easily and without bottleneck. Node internally uses multiple POSIX threads for various I/O operations such as File, DNS, Network calls etc.

When Node gets I/O request it creates or uses a thread to perform that I/O operation and once the operation is done, it pushes the result to the event queue. On each such event, event loop runs and checks the queue and if the execution stack of Node is empty then it adds the queue result to execution stack.

This is how Node manages concurrency.

Q5: What is global installation of dependencies?

Globally installed packages/dependencies are stored in /npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. To install a Node project globally use -g flag.

Q6: How can you avoid callback hells?

To do so you have more options:

  • modularization: break callbacks into independent functions
  • use Promises
  • use yield with Generators and/or Promises
Q7: What's the event loop?

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.



Every I/O requires a callback - once they are done they are pushed onto the event loop for execution. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.

Q8: How Node prevents blocking code?

By providing callback function. Callback function gets called whenever corresponding event triggered.

Q9: What is Event Emmitter?

All objects that emit events are members of EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
  console.log('an event occurred!');
});
myEmitter.emit('event');

Q10: What tools can be used to assure consistent code style?

You have plenty of options to do so:



These tools are really helpful when developing code in teams, to enforce a given style guide and to catch common errors using static analysis.

Q11: Provide some example 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:


 var config = {
 production: {
 mongo : { billing: '****' }
 },
 default: {
     mongo : { billing: '****' }
 }
 }
 exports.get = function get(env) { return config[env] || config.default; }
 

And it's usage:

const config = require('./config/config.js').get(process.env.NODE_ENV);
const dbconn = mongoose.createConnection(config.mongo.billing);



Saturday, October 20, 2018

Node.JS Interview Questions And Answers



Node.JS Interview Questions And Answers


Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Now a days NodeJs is very popular in the industry so, if you want to get hired in NodeJS interview, at least you should have good knowledge of Nodejs. Here we have collected some frequently asked questions of Nodejs. I hope you will get some help from these questions with answers.

Node.JS Interview Questions And Answers


 Q1) What is NodeJS?

NodeJS is javascript runtime environment that is built on chrome to enable lightweight and effective performance of server-side and networking applications in javascript. Node.js is a web application framework built on Google Chrome's JavaScript Engine (V8 Engine).

Node.js = Runtime Environment + JavaScript Library

Q2) What is NPM?

Node Package Manager(NPM). It is used to provide command line environment to install and manage NodeJS Packages, and NodeJS repositories that can be accessed at search.nodejs.org

Q3) Syntax to install modules using NPM. 

Syntax : $ npm install

Example: $ npm install express

var express = ('express'); 

Where express is Name of the module, 

express is JS file, that can be used in your module.

Q4) Mention types of npm modules available that are used very often.

Express, connect, socket.io and socketjs, pug, mongodb and mongojs, redis, lodash, forever, bluebird, moment are some of npm modules. 

Q5) Command to list modules installed in current npm. 

$ npm ls

Note: Remember not to mention npm in UPPERCASE - (Recommended) 

Q6) What are the attributes of package.json 

Name, version, description, homepage, author, contributors, dependencies − list of dependencies, repository − repository type and URL of the package, main − entry point of the package, keywords. 

Q7) What is REPL?

 REPL - Read, Eval, Print, Loop. 

It is an environment to input commands to perform REPL tasks. Each of the task is associated with its respective operations. 

Q8) What kind of applications can be built by Node.JS.

 DIRT, JSON, I/O Bound, Single Page applications are preferable to use Node.JS. 

Q9) What is the latest version of Node JS.

 Node v8.0.0 is the current version.

Q10) NodeJS is a framework. True or False. 

False. It is runtime or environment, but not a framework 

Q11) Commands to work with the file from the local database. 

 Var fs = require(‘fs’);
  • Read: fs.readFile(); 
  • Create: fs.appendFile(); fs.open(); fs.writeFile(); 
  • Write: fs. writeFile(); 
  • Delete: fs.unlink(); 
  • Rename: fs.rename(); 
  • Update: fs.appendFile() fs.writeFile()
Q12) What is error-first callback in Node JS? 

In order to check for proper working of the code, we need to verify error free execution. In this regard, error-first callbacks are used, that will send error first, followed by related data to the error. 

Q13) What is cluster? 

Cluster is a process to handle thread execution load while working with multi-core systems.

Q14) Why node is single threaded? 

In order to upgrade the performance well, single thread operations work better than multi-threaded operations. 

Q15) How does URL module works. 

This module helps URL to parse it into host, pathname, search, query, etc. 

Example: 
var url = require('url');
var adr = 'https://mindmajix.com/?s=node+js+training'; 
var q = url.parse(adr, true); 
console.log(q.host); //returns 'mindmajix.com' 
console.log(q.search); //returns '?s=node+js+training' 

Q16) Explain use of nodemailer module. 

It is not included in default modules list, it has to be installed using npm. 

Var nodemailer = require('nodemailer'); 

Q17) What are the types of versions available? 

Patch_Version, Minor_Version, Major_Version. 

Q18) What are event emitters in NodeJS? 

Objects in NodeJS will have to trigger events in order to maintain asynchronous execution of core API used. These objects that emit events are known as Event Emitters. 

Q19) What are event listeners? 

Events that are emitted by event emitters have to be listened in order to continue the asynchronous execution of the threads. These objects that listens the emitted events are known as listeners.