Interview questions for Node.js

Node.js the server-side scripting tool, uses JavaScript – a language that is popular with millions of developers worldwide – ensuring that it has a much lower learning curve even for complete beginners. Using Node.js, one can build everything, starting from a simple command line programs to the complex enterprise level web applications with equal ease.

1. What Is Node.js?

Node.js is a powerful framework developed on Chrome’s V8 JavaScript engine that compiles the JavaScript directly into the native machine code. It is a lightweight framework used for creating server-side web applications and extends JavaScript API to offer usual server-side functionalities. It is generally used for large-scale application development, especially for video streaming sites, single page application, and other web applications.

2. What is REPL in context of Node?

REPL stands for Read Eval Print Loop and it represents a computer environment like a window console or unix/linux shell where a command is entered and system responds with an output. Node.js or Node comes bundled with a REPL environment. It performs the following desired tasks.
• Read − Reads user’s input, parse the input into JavaScript data-structure and stores in memory.
• Eval − Takes and evaluates the data structure
• Print − Prints the result
• Loop − Loops the above command until user press ctrl-c twice

3. Why Node.js is single threaded?

Node.js uses a single threaded model in order to support async processing. With async processing, an application can perform better and is more scalable under web loads. Thus, Node.js makes use of a single-threaded model approach rather than typical thread-based implementation.

4. What is global installation of dependencies?

Globally installed packages/dependencies are stored in <user-directory>/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.

5. How do Node.js works?

Node.js is a virtual machine that uses JavaScript as its scripting language and runs on a v8 environment. It works on a single-threaded event loop and a non-blocking I/O which provides high rate as it can handle a higher number of concurrent requests. Also, by making use of the ‘HTTP’ module, Node.js can run on any stand-alone web server.

6. What is local installation of dependencies?

By default, npm installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require(). To install a Node project locally following is the syntax

7. Where Node.js can be used?

Node.js can be used to develop:
• Real-Time Web Applications
• Network Applications
• Distributed Systems
• General Purpose Applications

8. How many types of API functions are there in Node.js?

There are two types of API functions in Node.js:
• Asynchronous, non-blocking functions
• Synchronous, blocking functions

9 .What is Callback?

Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written is such a way that they supports callbacks. For example, a function to read a file may start reading file and return the control to execution environment immidiately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.

10. List down the tasks which should be done asynchronously using the event loop?

Below is the list of the tasks which must be done asynchronously using the event loop:
• I/O operations
• Heavy computation
• Anything requiring blocking

11. Name some of the flags used in read/write operation on files?

Flags for read/write operations are following:
• r − Open file for reading. An exception occurs if the file does not exist.
• r+ − Open file for reading and writing. An exception occurs if the file does not exist.
• rs − Open file for reading in synchronous mode. Instructs the operating system to bypass the local file system cache. This is primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don’t use this flag unless you need it. Note that this doesn’t turn fs.open() into a synchronous blocking call. If that’s what you want then you should be using fs.openSync()
• rs+ − Open file for reading and writing, telling the OS to open it synchronously. See notes for ‘rs’ about using this with caution.
• w − Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
• wx − Like ‘w’ but fails if path exists.
• w+ − Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
• wx+ − Like ‘w+’ but fails if path exists.
• a − Open file for appending. The file is created if it does not exist.
• ax − Like ‘a’ but fails if path exists.
• a+ − Open file for reading and appending. The file is created if it does not exist.
• ax+’ − Like ‘a+’ but fails if path exists.

12. List down the steps using which “Control Flow” controls the function calls in Node.js?

1. Control the order of execution
2. Collect data
3. Limit concurrency
4. Call the next step in the program

13. What do you understand by a test pyramid?

A test pyramid basically is a diagram that describes the ratio of how many unit tests, integration tests, and end-to-end test are required to be written for the successful development of the project.

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

leave your comment


Your email address will not be published. Required fields are marked *