Adnan Mohsin December 24, 2016 Comments 2.00

"Top Node.js Interview Questions Must read Article

1. What is Node.js?

Node.js is a platform for building fast, scalable network application. Its advantages over other server side languages is that it uses event-driven, non-blocking I/O model that makes it light-weight and efficient.

2. Can you explain how Node.js works?

It uses Google V8 Javascript engine to execute code. It contains built-in asynchronous I/O library for file, socket and HTTP communication. Node.js encapsulates libuv to handle asynchronous events.

3. Is Node.js really Single-Threaded?

Node.js operates on single-thread, but using non-blocking I/O calls allows it to support many concurrent connections. That means node doen’t process the requests in parallel but all the back-end stuffs which actually takes lot of time run in parallel.

4. Can you explain the Asynchronous approach in Node.js?

Nodejs operates asynchronously using event loop and callback functions.An Event Loop is a functionality which handles and processes all your external events and just converts them to a callback function. It invokes all your event handlers at a proper time. So, that means while executing a single request, it does a lot of things in the back-end so that the current request or the coming request doesn’t take much time.

5. Can you explain what is Globals in Node.js?

Global, Process and Buffer are combinedly termed as Globals.

Global : Its a global namespace object
Process : Its also a global object but it provides essential functionality to transform a synchronous function into a asynchronous callback.
Buffer : Raw data is stored in instances of the Buffer class.

6. What is the Use of underscore in Node.js?

To access the last expression, we have to use the (_) underscore/underline character.

7. Can you create Http Server in Node.js, explain with code

Yes, we can create Http Server in Nodejs. We can use http-server command to do so.

Code :

var http = require(‘http’);
var requestListener = function (request, response) {
response.writeHead(200, {‘Content-Type’: ‘text/plain’});
response.end(‘Hello You\n’);
}

var server = http.createServer(requestListener);
server.listen(8080); // The port where you want to start with.

8. How to load HTML in Node.js?

To load HTML in Nodejs we have to change the Content-type from text/plain to text/html.

9. Can you explain the difference between Node.js vs Ajax?

The difference between Node.js and Ajax is that Ajax is a client side technology whereas Nodejs is server side technology. Ajax is used for updating the contents of the page without refreshing it whereas Nodejs is used for developing server software. Nodejs is executed by the server whereas Ajax is executed by the browser.

10. Can you explain the difference between readFile vs createReadStream in Node.js?

readFile – It will read the file completely into memory before making it available to the User.

createReadStream – It will read the file in chunks of the size which is specified before hand.

11. 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.

12. 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.

13. 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.

14. 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.

15. 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.

16. What do you mean by the term I/O ?

I/O is the shorthand for input and output, and it will access anything outside of your application. It will be loaded into the machine memory to run the program, once the application is started.

17. What does event-driven programming mean?

In computer programming, event driven programming is a programming paradigm in which the flow of the program is determined by events like messages from other programs or threads. It is an application architecture technique divided into two sections 1) Event Selection 2) Event Handling

18. Where can we use node.js?

Node.js can be used for the following purposes

a)      Web applications ( especially real-time web apps )

b)      Network applications

c)       Distributed systems

d)      General purpose applications

19. What is the advantage of using node.js?

a)      It provides an easy way to build scalable network programs

b)      Generally fast

c)       Great concurrency

d)      Asynchronous everything

e)      Almost never blocks

 

20. What are the two types of API functions in Node.js ?

The two types of API functions in Node.js are

a)      Asynchronous, non-blocking functions

b)      Synchronous, blocking functions

21. What is control flow function?

A generic piece of code which runs in between several asynchronous function calls is known as control flow function.

22. Explain the steps how “Control Flow” controls the functions calls?

a)      Control the order of execution

b)      Collect data

c)       Limit concurrency

d)      Call the next step in program

23. Why Node.js is single threaded?

For async processing, Node.js was created explicitly as an experiment. It is believed that more performance and scalability can be achieved by doing async processing on a single thread under typical web loads than the typical thread based implementation.

24. Does node run on windows?

Yes – it does. Download the MSI installer from http://nodejs.org/download/

25. Can you access DOM in node?

No, you cannot access DOM in node.

26. Using the event loop what are the tasks that should be done asynchronously?

a)      I/O operations

b)      Heavy computation

c)       Anything requiring blocking

27.  Why node.js is quickly gaining attention from JAVA programmers?

Node.js is quickly gaining attention as it is a loop based server for JavaScript. Node.js gives user the ability to write the JavaScript on the server, which has access to things like HTTP stack, file I/O, TCP and databases.

28.  What are the two arguments that async.queue takes?

The two arguments that async.queue takes

a)      Task function

b)      Concurrency value

29.  What is an event loop in Node.js ?

To process and handle external events and to convert them into callback invocations an event loop is used. So, at I/O calls, node.js can switch from one request to another .

30.  Mention the steps by which you can async in Node.js?

By following steps you can async Node.js

a)      First class functions

b)      Function composition

c)       Callback Counters

d)      Event loops

31.  What are the pros and cons of Node.js?

Pros:

a)      If your application does not have any CPU intensive computation, you can build it in Javascript top to bottom, even down to the database level if you use JSON storage object DB like MongoDB.

b)      Crawlers receive a full-rendered HTML response, which is far more SEO friendly rather than a single page application or a websockets app run on top of Node.js.

Cons:

a)       Any intensive CPU computation will block node.js responsiveness, so a threaded platform is a better approach.
b)      Using relational database with Node.js is considered less favourable

32.  How Node.js overcomes the problem of blocking of I/O operations?

Node.js solves this problem by putting the event based model at its core, using an event loop instead of threads.

33.  What is the difference between Node.js vs Ajax?

The difference between Node.js and Ajax is that, Ajax (short for Asynchronous Javascript and XML) is a client side technology, often used for updating the contents of the page without refreshing it. While,Node.js is Server Side Javascript, used for developing server software. Node.js does not execute in the browser but by the server.

34.  What are the Challenges with Node.js ?

Emphasizing on the technical side, it’s a bit of challenge in Node.js to have one process with one thread to scale up on multi core server.

35.  What does it mean “non-blocking” in node.js?

In node.js “non-blocking” means that its IO is non-blocking.  Node uses “libuv” to handle its IO in a platform-agnostic way. On windows, it uses completion ports for unix it uses epoll or kqueue etc. So, it makes a non-blocking request and upon a request, it queues it within the event loop which call the JavaScript ‘callback’ on the main JavaScript thread.

36.  What is the command that is used in node.js to import external libraries?

Command “require” is used for importing external libraries, for example, “var http=require (“http”)”.  This will load the http library and the single exported object through the http variable.

37.  Mention the framework most commonly used in node.js?

“Express” is the most common framework used in node.js

38.  What is ‘Callback’ in node.js?

Callback function is used in node.js to deal with multiple requests made to the server. Like if you have a large file which is going to take a long time for a server to read and if you don’t want a server to get engage in reading that large file while dealing with other requests, call back function is used. Call back function allows the server to deal with pending request first and call a function when it is finished.

39. How does Node.js handle child threads?

Node.js, in its essence, is a single thread process. It does not expose child threads and thread management methods to the developer. Technically, Node.js does spawn child threads for certain tasks such as asynchronous I/O, but these run behind the scenes and do not execute any application JavaScript code, nor block the main event loop.

If threading support is desired in a Node.js application, there are tools available to enable it, such as the ChildProcess module.

40.  How does Node.js support multi-processor platforms, and does it fully utilize all processor resources?

Since Node.js is by default a single thread application, it will run on a single processor core and will not take full advantage of multiple core resources. However, Node.js provides support for deployment on multiple-core systems, to take greater advantage of the hardware. The Cluster module is one of the core Node.js modules and it allows running multiple Node.js worker processes that will share the same port.

Design
8/10
Support
2/10
Development
4.5/10

7.5

Awesome
3.50
Summary

Explore more books on Node.js for more on the subject.

Search widget

Banner

Latest posts

Design Patterns Design Patterns in Programming

What is a Design Pattern? According to MSDN, "A design pattern is a description of a set of interacting classes that provide a framework for a solution to a generalized problem in a specific context or environment. In other words, a pattern suggests a solution to a particular problem or issue in object-oriented software development.”

May 05, 2017 16 0

C# Ref and Out parameters in C Sharp

This tutorial explains the concepts of Ref and Out Keywords in C # programming. These two keywords are also frequently asked in most interviews.

January 15, 2017 16 0

WPF Vector Graphics in WPF

In this article, I will discuss how we can create a Line, Rectangle, ellipse and Polygon control in WPF at design-time using XAML and at run-time using C#.I will also explain their properties to format such as fill property. After that, i will show you to set an image as the background of any shape object.

January 08, 2017 16 0

C# What is the difference between an Abstract Class and an Interface

One question is always out there in most interviews. What is the difference between an Abstract Class and an Interface and why do you use one over the other? It seems like a simple question but most often, the answer to this question is not sufficient for the interviewer and can cost a job. So, I have decided to write an article about them and have tried to explain it.

December 26, 2016 16 0