Node.js Interview Questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What are async and await?

A function marked with the async keyword can contain an await expression. The await expression pauses the execution of the function and waits for the resolution of the passed Promise. It then resumes the function's execution and returns the resolved value.

What is __dirname?

A global variable which returns the absolute path of the directory containing the currently executing file.

What is Node.js' Cluster module?

A module used to take advantage of multi-core systems, so that apps can handle more load.

What do you mean by Asynchronous API?

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.

What is the difference between dependencies and devDependencies?

Both are defined in the package.json. dependencies lists the packages that the project is dependent on. devDependencies lists the dependencies which are only required during testing and development.

What is Chaining in Node?

Chanining is a mechanism to connect output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations.

What is the name of the JavaScript engine that Node.js is built upon?

Chrome's V8 engine.

What is difference between synchronous and asynchronous method of fs module?

Every method in fs module has synchronous as well as asynchronous form. Asynchronous methods takes a last parameter as completion function callback and first parameter of the callback function is error. It is preferred to use asynchronous method instead of synchronous method as former never block the program execution where the latter one does.

What are the names of at least three of the most popular Node.js frameworks?

Express, Koa, Hapi, Meteor, Sails, Loopback etc.

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.

What is a blocking code?

If application has to wait for some I/O operation in order to complete its execution any further then the code responsible for waiting is known as blocking code.

Which is the first argument usually passed to a Node.js callback handler?

It is an error object, which is null or undefined if an error did not occur.

What is the name of the file which npm uses to identify the project and its dependencies?

Its name is package.json.

What are the key features of Node.js?

Let's look at some of the key features of Node.js. Asynchronous event driven IO helps concurrent request handling - All APIs of Node.js are asynchronous. This feature means that if a Node receives a request for some Input/Output operation, it will execute that operation in the background and continue with the processing of other requests. Thus it will not wait for the response from the previous requests. Fast in Code execution - Node.js uses the V8 JavaScript Runtime engine, the one which is used by Google Chrome. Node has a wrapper over the JavaScript engine which makes the runtime engine much faster and hence processing of requests within Node.js also become faster. Single Threaded but Highly Scalable - Node.js uses a single thread model for event looping. The response from these events may or may not reach the server immediately. However, this does not block other operations. Thus making Node.js highly scalable. Traditional servers create limited threads to handle requests while Node.js creates a single thread that provides service to much larger numbers of such requests. Node.js library uses JavaScript - This is another important aspect of Node.js from the developer's point of view. The majority of developers are already well-versed in JavaScript. Hence, development in Node.js becomes easier for a developer who knows JavaScript. There is an Active and vibrant community for the Node.js framework - The active community always keeps the framework updated with the latest trends in the web development. No Buffering - Node.js applications never buffer any data. They simply output the data in chunks.

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

When should we use Node.js?

Node.js is well suited for applications that have a lot of concurrent connections and each request only needs very few CPU cycles, because the event loop (with all the other clients) is blocked during execution of a function. I believe Node.js is best suited for real-time applications: online games, collaboration tools, chat rooms, or anything where what one user (or robot? or sensor?) does with the application needs to be seen by other users immediately, without a page refresh.

Does Node.js operate in a single-threaded or multi-threaded fashion?

Node.js operates on a single thread.

What is the event loop?

Node.js processes incoming requests in the event loop. It is what allows Node.js to perform non-blocking operations despite the fact that JavaScript is single-threaded.

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.

What does "callback hell" mean?

Plenty of nested callbacks lead to callback hell. They make code difficult to read and maintain. One of the ways to deal with callback hell is to use the async/await syntax.

What is REPL and how can you access it from the command line?

REPL stands for "Read Eval Print Loop." It is an interface for easily running Node.js code. It can be started from the command line using the node command.

What is libuv?

The C library that implements the event loop and all of the asynchronous behaviors.

What keyword do you need to use to insert a debug breakpoint?

The debugger keyword.

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.

Are you familiar with differences between Node.js nodules and ES6 nodules?

The modules used in Node.js follow a module specification known as the CommonJS specification. The recent updates to the JavaScript programming language, in the form of ES6, specify changes to the language, adding things like new class syntax and a module system. This module system is different from Node.js modules. To import ES6 module, we'd use the ES6 import functionality. Now ES6 modules are incompatible with Node.js modules. This has to do with the way modules are loaded differently between the two formats. If you use a compiler like Babel, you can mix and match module formats.

Which global variable can be used to access information about the app and the environment that it runs in?

The process variable.

How are objects that generate events called and which class from the events module are they instances of?

These objects are called event emitters and are instances of the EventEmitter class.

What is a blocking function?

Unlike non-blocking functions, a blocking function's execution must be completed before other statements are executed.

Does the fs module, used for file-based operations, offer the possibility for synchronous or asynchronous reading and writing?

fs offers both synchronous and asynchronous methods for reading from and writing to files.

What are the two methods in the fs module that can be used for reading a whole file at once?

fs.readFile and fs.readFileSync.

What is libuv?

libuv is a C library that is used to abstract non-blocking I/O operations to a consistent interface across all supported platforms. It provides mechanisms to handle file system, DNS, network, child processes, pipes, signal handling, polling and streaming. It also includes a thread pool for offloading work for some things that can't be done asynchronously at the operating system level.

Which keywords are used to export and import an object or a function from a module file?

module.exports is used to export an object or a function. require is used to import one.

What is npm?

npm is Node.js' package ecosystem. It is the largest ecosystem of open-source libraries in the world. It is also the name of the command line package manager used to interact with npm.

What is the difference between setTimeout and setImmediate?

setTimeout is used to run a function after a certain minimal timeout. setImmediate is used to run a function once the current event loop completes.


संबंधित स्टडी सेट्स

8th Grade Spanish Study Guide for Proficiency Exam! (Blue Packet)

View Set

Check your understanding ch. 17 & ch. 20

View Set

Lifespan Final accumulative part

View Set

Chapter 4 - Casualty (Liability) Insurance Basics

View Set