a blog for those who code

Saturday, 3 October 2015

5 Interview Questions on Node.js

In this post we will be discussing about 5 Interview Questions on Node.js. The interview questions on Node.js is just to get acquainted with the nature of questions you may encounter during your interview. You may also go through 10 Interview Questions on Nodejs.


1. What is Callback in context of Node.js ?

A callback is an asynchronous equivalent for a function which is being called when a given task is completed. Node.js makes heavy use of callbacks, all the API's are written such as way that it supports callbacks. The advantage of using callback is that it makes Node.js highly scalable i.e. processing high number of request without waiting for any function to complete. 

2. What is Callback Hell and how to avoid it ?

Callback hell means a heavily nested callbacks which make the code unreadable and difficult to maintain. To avoid callback hell one should use modularization, which means breaking the callbacks into independent functions. Another method to avoid callback hell is use to use Promises, which allows error propagation and chaining.

3. What is Event Loop and Event Emitter ?

Node.js supports concurrency with the help of events and callbacks even if it is single threaded application. Node thread keeps an event loop and whenever any task gets completed, that thread fires the corresponding event.

EventEmitter fires an event whenever any tasks gets completed, any error occurred, any new listener is added or any listener is removed. It provides properties like on and emit, where on is used to bind the function and emit is used to fire an event.

4. How many types of Streams are present in Node.js ?

There are four types of streams are present in Node.js and they are Readable, Writable, Duplex and Transform. Readable stream is used for read operation, Writable for write operation, Duplex for both read and write operation and Transform is type of Duplex stream where output is computed based on input.

5. Why to use Buffers instead of binary strings to handle binary data ?

Pure JavaScript does not able to handle straight binary data very well. Since Node.js servers have to deal with TCP streams for reading and writing of data, binary strings will become problematic to work with as it is very slow and has a tendency to break. That's why it is always advisable to use Buffers instead of binary strings to handle binary data.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful. 

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License