YouTube Icon

Interview Questions.

Top 50 Node.js Interview Questions - Jul 26, 2022

fluid

Top 50 Node.js Interview Questions

Q1. What Is Event Loop?

Node js is a single threaded utility however it help concurrency through concept of occasion and callbacks. As every API of Node js are asynchronous and being a unmarried thread, it uses async function calls to keep the concurrency. Node uses observer pattern. Node thread keeps an occasion loop and each time any challenge get finished, it fires the corresponding occasion which indicators the event listener feature to get finished.

 

Q2. What Is Global Installation Of Dependencies?

Globally hooked up programs/dependencies are stored in <user-directory>/npm listing. Such dependencies may be used in CLI (Command Line Interface) characteristic of any node.Js but cannot be imported the use of require() in Node application without delay. To installation a Node venture globally use -g flag.

C:Nodejs_WorkSpace>npm installation specific -g

 

Q3. What Is The Use Of Underscore Variable In Repl?

Use _ to get the final end result.

C:Nodejs_WorkSpace>node

> var x = 10

undefined

> var y = 20

undefined

> x + y

30

> var sum = _

undefined

> console.Log(sum)

30

undefined

>

Q4. Name Some Of The Attributes Of Package.Json?

Following are the attributes of Package.Json

call - name of the package deal

model - model of the bundle

description - description of the package deal

homepage - homepage of the bundle

creator - creator of the package

members - call of the members to the package deal

dependencies - list of dependencies. Npm robotically installs all the dependencies referred to here inside the node_module folder of the package deal.

Repository - repository kind and url of the package deal

important - access factor of the package

key phrases - keywords

Q5. What Is Repl In Context Of Node?

REPL stands for Read Eval Print Loop and it represents a computer surroundings like a window console or unix/linux shell in which a command is entered and machine responds with an output. Node.Js or Node comes bundled with a REPL surroundings. It performs the subsequent favored duties.

Read - Reads user's enter, parse the input into JavaScript statistics-shape and stores in reminiscence.

Eval - Takes and evaluates the data structure

Print - Prints the result

Loop - Loops the above command until user press ctrl-c twice.

Q6. What Is Event Emmitter?

EventEmitter elegance lies in activities module. It is accessibly through following syntax:

//import occasions module

var events = require('events');

//create an eventEmitter item

var eventEmitter = new occasions.EventEmitter();

When an EventEmitter example faces any error, it emits an 'mistakes' event. When new listener is added, 'newListener' occasion is fired and whilst a listener is removed, 'removeListener' event is fired.

EventEmitter presents a couple of houses like on and emit. On property is used to bind a characteristic with the occasion and emit is used to hearth an event.

Q7. How Will You Truncate A File Using Node?

Following is the syntax of the technique to truncate an opened document:

fs.Ftruncate(fd, len, callback)

Parameters

Here is the outline of the parameters used:

fd - This is the document descriptor back through document fs.Open() method.

Len - This is the duration of the record after which report might be truncated.

Callback - This is the callback feature which receives no arguments other than a possible exception are given to the of entirety callback.

 

Q8. How To Check The Already Installed Dependencies Which Are Globally Installed Using Npm?

Use the subsequent command:

C:Nodejs_WorkSpace>npm ls -g

Q9. What Is Node.Js?

Node.Js is a web software framework constructed on Google Chrome's JavaScript Engine(V8 Engine).

Node.Js comes with runtime environment on which a Javascript based totally script may be interpreted and done (It is analogus to JVM to JAVA byte code). This runtime lets in to execute a JavaScript code on any system outside a browser. Because of this runtime of Node.Js, JavaScript is now may be completed on server as well.

Node.Js additionally gives a wealthy library of various javascript modules which eases the developement of net application using Node.Js to wonderful extents.

Node.Js = Runtime Environment + JavaScript Library

Q10. Name Some Of The Flags Used In Read/write Operation On Files.

Flags for study/write operations are following:

r - Open file for reading. An exception happens if the report does now not exist.

R+ - Open report for reading and writing. An exception takes place if the file does not exist.

Rs - Open file for reading in synchronous mode. Instructs the running device to bypass the nearby file gadget cache. This is ordinarily beneficial for opening files on NFS mounts because it permits you to bypass the potentially stale neighborhood cache. It has a very real impact on I/O performance so do not use this flag until you want it. Note that this does not flip fs.Open() into a synchronous blocking off call. If that is what you want then you need to be the usage of fs.OpenSync()

rs+ - Open file for reading and writing, telling the OS to open it synchronously. See notes for 'rs' about using this with warning.

W - Open file for writing. The report is created (if it does now not exist) or truncated (if it exists).

Wx - Like 'w' however fails if path exists.

W+ - Open document for analyzing and writing. The report is created (if it does now not exist) or truncated (if it exists).

Wx+ - Like 'w+' but fails if path exists.

A - Open file for appending. The record is created if it does no longer exist.

Ax - Like 'a' however fails if path exists.

A+ - Open document for reading and appending. The report is created if it does not exist.

Ax+' - Like 'a+' however fails if route exists.

Q11. What Is Piping In Node?

Piping is a mechanism to attach output of one circulate to every other circulate. It is generally used to get information from one flow and to pass output of that circulate to every other flow. There is no restrict on piping operations. Consider the above instance, in which we have read test.Txt the use of readerStream and write test1.Txt the usage of writerStream. Now we'll use the piping to simplify our operation or reading from one document and writing to any other record.

Q12. What Are The Benefits Of Using Node.Js?

Following are essential blessings of the use of Node.Js

Aynchronous and Event Driven All APIs of Node.Js library are aynchronous this is non-blocking off. It basically me a Node.Js primarily based server never waits for a API to go back statistics. Server moves to next API after calling it and a notification mechanism of Events of Node.Js allows server to get reaction from the preceding API name.

Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.Js library may be very rapid in code execution.

Single Threaded however fairly Scalable - Node.Js makes use of a single threaded model with event looping. Event mechanism facilitates server to reply in a non-bloking approaches and makes server tremendously scalable as antagonistic to standard servers which create constrained threads to address requests. Node.Js makes use of a unmarried threaded program and identical software can services an awful lot large wide variety of requests than traditional server like Apache HTTP Server.

No Buffering - Node.Js packages by no means buffer any facts. These programs absolutely output the facts in chunks.

Q13. How Will You Close A File Using Node?

Following is the syntax of one of the strategies to close an opened record:

fs.Close(fd, callback)

Parameters

Here is the description of the parameters used:

fd - This is the report descriptor back by using file fs.Open() technique.

Callback - This is the callback function which gets no arguments other than a likely exception are given to the crowning glory callback.

Q14. What Is Chaining In Node?

Chanining is a mechanism to attach output of 1 movement to some other circulate and create a series of a couple of circulate operations. It is typically used with piping operations.

Q15. What Is Callback?

Callback is an asynchronous equivalent for a function. A callback function is called at the finishing touch of a given challenge. Node makes heavy use of callbacks. All APIs of Node are written is the sort of way that they helps callbacks. For instance, a characteristic to examine a record might also begin studying record and return the manipulate to execution environment immidiately so that next preparation can be accomplished. Once record I/O is entire, it's going to call the callback feature whilst passing the callback feature, the content material of the record as parameter. So there's no blocking off or watch for File I/O. This makes Node.Js distinctly scalable, as it could technique excessive number of request with out looking forward to any function to go back end result.

Q16. What Is Purpose Of Buffer Class In Node?

Buffer magnificence is a worldwide elegance and can be accessed in application with out importing buffer module. A Buffer is a form of an array of integers and corresponds to a uncooked reminiscence allocation outdoor the V8 heap. A Buffer can not be resized.

Q17. Is Node A Single Threaded Application?

Yes! Node makes use of a unmarried threaded model with event looping.

Q18. Name Some Of The Events Fired By Streams.

Each kind of Stream is an EventEmitter instance and throws numerous events at one of a kind instance of times. For example, a number of the typically used occasions are:

data - This occasion is fired whilst there may be statistics is to be had to examine.

Cease - This event is fired when there is no greater statistics to study.

Error - This event is fired whilst there's any mistakes receiving or writing records.

End - This event is fired while all information has been flushed to underlying system

Q19. How Will You Read A Directory?

Following is the syntax of the approach to study a directory:

fs.Readdir(direction, callback)

Parameters

Here is the outline of the parameters used:

direction - This is the directory call together with route.

Callback - This is the callback function which gets  arguments (err, files) in which files is an array of the names of the documents within the directory except '.' and '..'.

 

Q20. What Is The Purpose Of Cleartimeout Function?

The clearTimeout( t ) worldwide characteristic is used to stop a timer that became previously created with setTimeout(). Here t is the timer again through setTimeout() function.

Q21. Is It Free To Use Node.Js?

Yes! Node.Js is released below the MIT license and is free to apply.

Q22. How Many Types Of Streams Are Present In Node?

In Node.Js, there are four kinds of streams.

Readable - Stream that's used for study operation.

Writable - Stream that's used for write operation.

Duplex - Stream which can be used for each examine and write operation.

Trform - A kind of duplex flow in which the output is computed based totally on enter.

Q23. Which Module Is Used For Web Based Operations?

Http module is used for internet based totally operations.

Var http = require("http")

Q24. What Is The Purpose Of __filename Variable?

The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a chief application this isn't always always the identical filename used inside the command line. The fee interior a module is the path to that module record.

 

Q25. What Is Npm?

Npm stands for Node Package Manager. Npm provides following two most important functionalities:

Online repositories for node.Js packages/modules which might be searchable on seek.Nodejs.Org

Command line software to install packages, do version control and dependency control of Node.Js applications.

 

Q26. How Will You Delete A File Using Node?

Following is the syntax of the approach to delete a report:

fs.Unlink(direction, callback)

Parameters

Here is the outline of the parameters used:

course - This is the document call which include route.

Callback - This is the callback feature which receives no arguments other than a likely exception are given to the of entirety callback.

Q27. What Do You Mean By Asynchronous Api?

All APIs of Node.Js library are aynchronous this is non-blockading. It basically me a Node.Js primarily based server never waits for a API to go back records. Server moves to subsequent API after calling it and a notification mechanism of Events of Node.Js enables server to get response from the previous API call.

Q28. What Is Local Installation Of Dependencies?

By default, npm installs any dependency in the neighborhood mode. Here neighborhood mode refers to the package deal installation in node_modules directory lying within the folder where Node software is gift. Locally deployed applications are on hand thru require(). To deploy a Node venture regionally following is the syntax.

C:Nodejs_WorkSpace>npm installation express

Q29. What Is A Blocking Code?

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

 

Q30. Which Module Is Used For Buffer Based Operations?

Buffer module is used for buffer based totally operations.

Var buffer = require("buffer")

Q31. What Are Streams?

Streams are gadgets that let you examine statistics from a supply or write records to a vacation spot in continous style.

Q32. What Is Difference Between Synchronous And Asynchronous Method Of Fs Module?

Every method in fs module have synchronous in addition to asynchronous shape. Asynchronous strategies takes a final parameter as crowning glory characteristic callback and primary parameter of the callback characteristic is errors. It is desired to apply asynchronous method as opposed to synchronous method as former never block this system execution where the latter one does.

Q33. What Is The Purpose Of Process Object?

Technique object is used to get statistics on modern-day technique. Provides multiple occasions related to method activities.

Q34. What Is Package.Json?

Bundle.Json is present within the root directory of any Node software/module and is used to define the homes of a package deal.

Q35. What Is The Purpose Of __dirname Variable?

The __dirname represents the name of the listing that the currently executing script is living in.

Q36. How Will You Open A File Using Node?

Following is the syntax of the method to open a file in asynchronous mode:

fs.Open(direction, flags[, mode], callback)

Parameters

Here is the description of the parameters used:

direction - This is string having file call together with course.

Flags - Flag tells the behavior of the file to be opened. All possible values have been cited beneath.

Mode - This sets the document mode (permission and sticky bits), however handiest if the report turned into created. It defaults to 0666, readable and writeable.

Callback - This is the callback feature which gets two arguments (err, fd).

Q37. How Will You Write A File Using Node?

Following is the syntax of one of the methods to put in writing right into a record:

fs.WriteFile(filename, records[, options], callback)

This technique will over-write the record if record already exists. If you want to put in writing into an existing report then you definitely have to use some other approach to be had.

Parameters

Here is the description of the parameters used:

route - This is string having report name such as direction.

Facts - This is the String or Buffer to be written into the report.

Options - The 1/3 parameter is an item a good way to hold encoding, mode, flag. By default encoding is utf8, mode is octal fee 0666 and flag is 'w'

callback - This is the callback feature which receives a single parameter err and used to to go back error in case of any writing blunders.

Q38. How Will You Delete A Directory?

Following is the syntax of the technique to remove a directory:

fs.Rmdir(direction, callback)

Parameters

Here is the description of the parameters used:

direction - This is the listing call consisting of route.

Callback - This is the callback characteristic which gets no arguments apart from a possible exception are given to the of completion callback.

 

Q39. How Will You Read A File Using Node?

Following is the syntax of one of the methods to examine from a document:

fs.Study(fd, buffer, offset, duration, function, callback)

This method will use record descriptor to study the report, in case you need to read report the usage of file call directly then you must use every other approach to be had.

Parameters

Here is the outline of the parameters used:

fd - This is the document descriptor lower back with the aid of record fs.Open() approach.

Buffer - This is the buffer that the records could be written to.

Offset - This is the offset within the buffer to begin writing at.

Period - This is an integer specifying the variety of bytes to read.

Function - This is an integer specifying wherein to start reading from inside the file. If function is null, data may be read from the present day file position.

Callback - This is the callback feature which gets the three arguments, (err, bytesRead, buffer).

 

Q40. What Is The Purpose Of Settimeout Function?

The setTimeout(cb, ms) worldwide feature is used to run callback cb after at least ms milliseconds. The actual postpone depends on outside factors like OS timer granularity and gadget load. A timer can not span extra than 24.8 days.

This feature returns an opaque cost that represents the timer which may be used to clean the timer.

Q41. Which Module Is Used For File Based Operations?

Fs module is used for file based totally operations.

Var fs = require("fs")

Q42. How Will You Get Information About A File Using Node?

Following is the syntax of the approach to get the statistics approximately a record:

fs.Stat(course, callback)

Parameters

Here is the outline of the parameters used:

direction - This is string having file name inclusive of direction.

Callback - This is the callback function which gets two arguments (err, stats) where stats is an item of fs.Stats kind which is printed underneath in the example.

 

Q43. What Is The Purpose Of Console Object?

Console item is used to Used to print information on stdout and stderr.

Q44. How To Uninstall A Dependency Using Npm?

Use following command to uninstall a module.

C:Nodejs_WorkSpace>npm uninstall dependency-call

Q45. How Node Prevents Blocking Code?

By imparting callback characteristic. Callback feature receives called each time corresponding event triggered.

Q46. What Is The Purpose Of Setinterval Function?

The setInterval(cb, ms) international feature is used to run callback cb again and again after at least ms milliseconds. The actual put off depends on external elements like OS timer granularity and gadget load. A timer cannot span more than 24.8 days.

This feature returns an opaque cost that represents the timer which can be used to clear the timer the usage of the feature clearInterval(t).

Q47. How Will You Create A Directory?

Following is the syntax of the method to create a listing:

fs.Mkdir(path[, mode], callback)

Parameters

Here is the description of the parameters used:

course - This is the directory call which includes course.

Mode - This is the listing permission to be set. Defaults to 0777.

Callback - This is the callback feature which receives no arguments other than a probable exception are given to the of entirety callback.

Q48. Why To Use Node.Js?

It is used to broaden I/O extensive internet applications like video streaming web sites, unmarried web page applications (SPA) and other web applications. Node.Js is open source and utilized by hundreds of builders around the arena.

Q49. How To Update A Dependency Using Npm?

Update package deal.Json and trade the model of the dependency which to be updated and run the subsequent command.

C:Nodejs_WorkSpace>npm replace

Q50. What Is The Difference Of Using Var And Not Using Var In Repl While Dealing With Variables?

Use variables to save values and print later. If var keyword isn't always used then fee is saved within the variable and published. Wheras if var key-word is used then fee is stored however not printed. You can use both variables later.




CFG