fbpx
Select Page

Interview Question & Answers on Node Js

Dear Readers, this is my new blog on Interview Question & Answers on Node Js.  This is my little effort to share my best knowledge with you. So I am making this blog format like a little conversation on node js topics. I hope it will be helpful for your interview preparation for various designations like MEAN stack developer, full-stack developer, web developer, Node js developer. 

 Q1. What is NodeJs?

Ans – Node js is a JavaScript framework developed on the chrome V8 engine. It helps to convert javascript code into native machine code. It is used to develop single-page applications, large scale applications, server-side applications.

Q2. What is the difference between Javascript and Node Js?

Ans – Javascript is a programming language used for the client-side web application. While node js is a javascript framework used for the server-side application, non -blocking I/O operation. Javascript running on various browser engines like spider monkey(firefox), V8(chrome), javascript core(safari), while node js running on the V8 engine.

Q3. What is npm?

Ans – The npm is a node package manager, that creates package manager for node.js. Npm can manage and install all the dependencies of the project, which are defined in the file package.json. Package.json file written in JSON format. 

Q4. What is the role of REPL in node.js?

Ans –  REPL means Read Eval Print Loop. which performs given below task

Read – Read the user’s input, parse it into javascript and store it in memory.

Eval – It takes and evaluates data.

Print – Prints the output.

Loop – loops the above command, until you can’t terminate by ctrl + c command.

REPL is a simple feature that tests node.js or javascript code.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Q.5 what are the types of API functions in node.js?

Ans – The types of API function in node.js are blocking function which executes synchronously and another is a non-blocking function that executes asynchronously.

The Blocking function executes synchronously means the event loop is unable to continue running JavaScript until a non-JavaScript operation completes.

For Example –

const fs = require(‘fs’);

const data = fs.readFileSync(‘./myfile.md’); 

In the above code myfile.md block, until the file is read.

Non-blocking function execute synchronously means it accepts callback function to execute without being halted.

For Example –

const fs = require(‘fs’);

fs.readFile(‘./file.md’, (err, data) => {

  if (err) throw err;

}); // does not have to wait for file read

Q.6 What is the difference between angular and node js?

Ans – Angular is a front-end framework that runs on the client browser. While node js is a back-end framework that runs on the server-side.

Q.7 What is the file system module in node js?

Ans – File system module in node js is used to handle physical files on your computer.

By including a file system module in your project you can perform various operation likes,

  • Reading file
  • Update file
  • Rename file
  • Creating file
  • Delete file

Q.8 What is an error-first callback in node js?

Ans – In most of the asynchronous function error-first callback is referred. In error-first callback function we pass a two-argument, first is an error then another is data. The first argument executes when something goes wrong, otherwise, it will execute second argument data.

errorFirstCallback = (err, data) => {

if(err){

console.error(‘There was an error’, err);

  return;

  }

  console.log(data);

};

fs.readFile(filepath, errorFirstCallback);

Q.9 What is the first argument passed to the node js callback handler?

Ans – The first argument passed to node js callback handler is a mostly optional error object. Otherwise, we can pass a null or undefined argument, if an error argument is not required.

Q.10 What are the benefits of using node js?

Ans – Following are the benefits of using node js.

  • Asynchronous – Node js is asynchronous programming meaning it is non-blocking. It’s never waiting for the API to return data from the server.
  • Event-Driven – node js uses events heavily. In this programming application, the main loop listens to events and triggers a callback function when an actual event occurs.
  • Fast and Highly Scalable –  Node js is built on google chrome V8 engine, which converts JavaScript code into machine code. So it helps to build fast and highly scalable network applications.
  • Single-threaded – Node js uses a single-threaded program to handle a number of requests at a time.
  • No Buffering – Node js never buffering any data, simply output data in chunks.

Q.11 What are the streams? And why use streams in node js?

Ans – streams are one of the data handling methods in node js. It is used to reading or writing files or any end-to-end information exchange in a simple way.

We prefer streams in node js because it has the following benefits –

  1. Memory Efficiency 
  2. Time Efficiency

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Q.12 Explain the types of streams?

Ans – There are four basic types of streams.

  1. Writable – we can write data to this stream. 

For example

fs.createWriteStream()

  1. Readable – we can read data from this stream. 

For example

fs.createReadStream()

  1. Duplex – we can read and write data from this stream. 

For example

net.Socket

  1. Transform – these are duplex streams which can modify or transform as it is written or read.

Q.13 What is the path module in node js?

Ans – Path module is working with directories and file paths. If you want to include a path module in your application you can write the following code.

var path = require(‘path’);

For example

If you want to extract the filename from a file path 

var path = require(‘path’);

var filename = path.basename(‘/myapp/demo_path.js’);

console.log(filename);

Q.14 How to use the file system(fs) module in an application? What are the tasks performed by the file system?

Ans – file system(fs) module provides API to interact with your files on your system. It includes the following code in your application. 

For Example

var fs = require(‘fs’);

By a file system(fs) module you can perform various tasks such as reading a file, opening a file, writing a file, and deleting a file.

  1. Reading file – 

You can read files by the following method.

fs.readFile(‘demofile.html’);

  1. Opening file – 

You can open files by the following method.

fs.open(“demofile.txt’’);

  1. Writing file – 

You can write files by the following method.

fs.writeFile(“demofile.txt’’, “welcome to home”);

  1. Deleting file – 

You can delete files by the following method.

fs.unlink(“demofile.txt’’);

  1. Rename file – 

You can rename files by the following method.

fs.rename(“demofile.txt’’, “newfile1.txt’’);

Q.15 How can you upload a file in an application?

Ans – If you want to create a file upload button or link in your application, then you have to install a formidable module in your project. And then you have to include a formidable module in your application.

var formidable = require(‘formidable’);

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Q.16 What is the express?

Ans – Express is a node js web application framework. Express used to develop web applications like server, REST API, and streaming engine in a node application. It also provides strong features to develop web and mobile applications.

Q.17 What is the timer? Explain the setTimeout, setInterval and clearTimeout timer?

Ans – A timer in node js executes global API after some specified period of time. Timer functions are global because it does not need to call require(‘timer’) to use.

  1. setTimeout() function executes output after waiting for a specified duration such as the number of milliseconds.

For Example 

setTimeout(4500,  () => {

console.log(‘ Hello Everybody!’);

});

  1. setInterval() function executes output continuously after waiting for a specified duration (no. of milliseconds) of interval. It repeats output continuously after the specified interval.

For Example 

setInterval(2500,  () => {

console.log(‘ Hello Everybody!’);

});

  1. clearTimeout() function cancels setTimeout() function and prevents it from executes.

Q.18 What is the role of the package.json file?

Ans – package.json file is a project root file that contains all npm packages. This file holds metadata such as project description, license information, version of the project, even configuring data of the project. The main role of this file provides information about the project to npm and handles the project dependencies.

Q.19 In which language node js is written? And what is the Javascript engine?

Ans – Node js is written in C, C++, and Javascript. A javascript engine is a program that converts javascript code into machine code and shows output on the browser.  Node js javascript engine is built on google’s V8 javascript engine.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Q.20 How can you check the version of node js in your pc?

Ans – I can check the existing version of node js by running command node -v in terminal.

Q.21 Explain the module in node js?

Ans – In node js, each file is treated as an individual module. Modules are reusable blocks of code. Suppose if I want to create a separate module then I first create file name area.js then I write the code in this file.

For Example

area.js file

const { PI } = Math;

exports.area = (r) => PI * r **2;

In the above code area module export the function area in the app module. So I imported the area module in the app module by following the code.

const circle = require(‘./area.js’);

console.log(`The area of a circle of radius 7 is {circle.area(7)}`);

 

Author-
Prasanna Kadu | SevenMentor Pvt Ltd.

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

Pin It on Pinterest

× How can I help you?