Node - Js - Interview Questions - Tutorialspoint
Node - Js - Interview Questions - Tutorialspoint
Dear readers, these Node.JS Interview Questions have been designed specially to get you
acquainted with the nature of questions you may encounter during your interview for the subject
of Node.JS. As per my experience good interviewers hardly plan to ask any particular question
during your interview, normally questions start with some basic concept of the subject and later
they continue based on further discussion and what you answer:
What is Node.js?
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 also provides a rich library of various javascript modules which eases the developement
of web application using Node.js to great extents.
Node.js = Runtime Environment + JavaScript Library
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.
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.
Yes! Node.js is released under the MIT license and is free to use.
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.
Yes.
What is the difference of using var and not using var in REPL while
dealing with variables?
Use variables to store values and print later. if var keyword is not used then value is stored in
the variable and printed. Whereas if var keyword is used then value is stored but not printed.
You can use both variables later.
C:\Nodejs_WorkSpace>node
> var x = 10
undefined
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 2/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log(sum)
30
undefined
>
What is npm?
npm stands for Node Package Manager. npm provides following two main functionalities:
Online repositories for node.js packages/modules which are searchable on
search.nodejs.org
Command line utility to install packages, do version management and dependency
management of Node.js packages.
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.
C:\Nodejs_WorkSpace>npm ls -g
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 3/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
What is Package.json?
package.json is present in the root directory of any Node application/module and is used to
define the properties of a package.
Update package.json and change the version of the dependency which to be updated and run
the following command.
C:\Nodejs_WorkSpace>npm update
What is Callback?
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.
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.
By providing callback function. Callback function gets called whenever corresponding event
triggered.
Node js is a single threaded application but it support concurrency via concept of event and
callbacks. As every API of Node js are asynchronous and being a single thread, it uses async
function calls to maintain the concurrency. Node uses observer pattern. Node thread keeps an
event loop and whenever any task get completed, it fires the corresponding event which signals
the event listener function to get executed.
When an EventEmitter instance faces any error, it emits an 'error' event. When new listener is
added, 'newListener' event is fired and when a listener is removed, 'removeListener' event is
fired.
EventEmitter provides multiple properties like on and emit. on property is used to bind a
function with the event and emit is used to fire an event.
Buffer class is a global class and can be accessed in application without importing buffer
module. A Buffer is a kind of an array of integers and corresponds to a raw memory allocation
outside the V8 heap. A Buffer cannot be resized.
Piping is a mechanism to connect output of one stream to another stream. It is normally used to
get data from one stream and to pass output of that stream to another stream. There is no limit
on piping operations. Consider the above example, where we've read test.txt using
readerStream and write test1.txt using writerStream. Now we'll use the piping to simplify our
operation or reading from one file and writing to another file.
var fs = require("fs")
true.
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 6/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
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).
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.
Streams are objects that let you read data from a source or write data to a destination in
continuous fashion.
Transform − A type of duplex stream where the output is computed based on input.
Each type of Stream is an EventEmitter instance and throws several events at different
instance of times. For example, some of the commonly used events are:
data − This event is fired when there is data is available to read.
end − This event is fired when there is no more data to read.
error − This event is fired when there is any error receiving or writing data.
finish − This event is fired when all data has been flushed to underlying system
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 7/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
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.
Parameters
This method will use file descriptor to read the file, if you want to read file using file name
directly then you should use another method available.
Parameters
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 8/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
This method will over-write the file if file already exists. If you want to write into an existing file
then you should use another method available.
Parameters
fs.close(fd, callback)
Parameters
callback − This is the callback function which gets no arguments other than a possible
exception are given to the completion callback.
Following is the syntax of the method to get the information about a file:
fs.stat(path, callback)
Parameters
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 9/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
callback − This is the callback function which gets two arguments (err, stats) where
stats is an object of fs.Stats type which is printed below in the example.
Parameters
len − This is the length of the file after which file will be truncated.
callback − This is the callback function which gets no arguments other than a possible
exception are given to the completion callback.
fs.unlink(path, callback)
Parameters
Parameters
callback − This is the callback function which gets no arguments other than a possible
exception are given to the completion callback.
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 10/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
fs.rmdir(path, callback)
Parameters
fs.readdir(path, callback)
Parameters
callback − This is the callback function which gets two arguments (err, files) where files
is an array of the names of the files in the directory excluding '.' and '..'.
The __filename represents the filename of the code being executed. This is the resolved
absolute path of this code file. For a main program this is not necessarily the same filename
used in the command line. The value inside a module is the path to that module file.
The __dirname represents the name of the directory that the currently executing script resides
in.
The setTimeout(cb, ms) global function is used to run callback cb after at least ms milliseconds.
The actual delay depends on external factors like OS timer granularity and system load. A timer
cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the
timer.
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 11/12
2/24/2021 Node.js - Interview Questions - Tutorialspoint
The clearTimeout( t ) global function is used to stop a timer that was previously created with
setTimeout(). Here t is the timer returned by setTimeout() function.
The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms
milliseconds. The actual delay depends on external factors like OS timer granularity and system
load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the
timer using the function clearInterval(t).
process object is used to get information on current process. Provides multiple events related to
process activities.
What is Next ?
Further you can go through your past assignments you have done with the subject and make
sure you are able to speak confidently on them. If you are fresher then interviewer does not
expect you will answer very complex questions, rather you have to make your basics concepts
very strong.
Second it really doesn't matter much if you could not answer few questions but it matters that
whatever you answered, you must have answered with confidence. So just feel confident during
your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the
very best for your future endeavor. Cheers :-)
https://www.tutorialspoint.com/nodejs/nodejs_interview_questions.htm 12/12