Nodejs Interview Questions and Answers

Interactive Online Courses: Elevate Skills & Succeed Enroll Now!

In this article, you will have intervie3w questions and answers for different nodejs methods and modules. This includes theoretical as well as coding questions on node js.

Nodejs Interview Questions and Answers

1) Demonstrate how a raw listener works programmatically.
Ans. Below is the code:

let EventEmitter = require('events');
let emitter = new EventEmitter();
function event(arg) {
    console.log('Welcome to', arg.name);
}
emitter.on('event', event)
emitter.emit('event', { name: "DataFlair" })
console.log(emitter.rawListeners('event'))
emitter.off('event', event)

2) Can variables be used in node js terminal? If yes, how?
Ans. Yes, we can store values in variables in node js terminal. There are two ways to store variables:

Directly assigning to a variable Eg: a=20.
Or
Var a=20.

When we use the first method then the value automatically gets printed, whereas when we use the second method “undefined” is initially shown. When we again access the variable, its correct value is shown.

3) Explain is it possible to use multiline statements in the repl terminal?
Ans. We can write multiline code in the repl terminal and then execute it in the repl terminal. To write such statements we use this ‘{‘ bracket then we can press enter.

Now you will see ‘…’ which indicates that you are in the multiline expressions and you can now write your statements.

4)What are the useful commands for node js terminal?
Ans
1) Use ctrl+c once: Terminate current command.
2) Use ctrl+c twice: Quit the repl terminal.
3) Use ctrl+d : Quit the repl terminal.
4) Up/down key: Shows previous commands.
5) Tab key: View current commands
6) .help: Gives a list of all commands that it supports
7) .clear: Exits the multiline expression.
8) .break: Exits from multiline command
9) .save “filename”: Saves the current repl as a file.
10) .load “filename”: Loads a file into repl terminal

5) What are the timing features of node js?
Ans

  • setTimeout– implements delays in code execution.
  • setInterval– runs a code block multiple times.
  • setImmediate– sets the execution of the code at the end of the event loop cycle.
  • process.nextTick – sets the execution of code at the beginning of the next event loop cycle

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

6)What are the main advantages of promises over callback?
Ans Promises get an object to decide the action that needs to be done after the async task gets complete. This gives more manageable code and reduces callback hell. It also provides better error handling and asynchronous codes easily.

7) What is call back hell?
Ans.When a call back function is passed inside a function and again in the callback function there exists another call back function and so on.Then the code becomes unreadable and not maintainable the code goes on shifting to right side. So callback hell is a situation when a function takes a callback which inturn takes another callback function and so on.

8) How to use a formidable module to upload the file?
Ans. We have to include it using the require function. The below code demonstrates how to use formidable module to upload the file

let http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload">');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
}).listen(5000);

9) If we want to use a hosted api for sending emails what method should we use?
Ans. It allows us to send email from our program using an API. We can use the API to handle sending and delivering the email. These APIs give a reliable service which can be used in our program easily with better functionality. One disadvantage of this method is that we have to rely on third parties for sending mails.

10) Why should we not use a transactional email api?
Ans: Mainly because of the below reasons we should not prefer transactional email api

  • We have to rely on third-party for sending mails
  • We have to separately integrate each channel.
  • You don’t have ownership for the email service.

11) Differentiate between synchronous and async functions.
Ans Functions that do not block other codes until it is completely executed are called asynchronous function.
Synchronous function blocks the thread until this function executes completely.

Reading a file in asynchronous mode will not block other functions but if we use a synchronous method to read a file then all other functions will get blocked.

12) Differentiate between blocking and non blocking callbacks.
Ans. Codes that block other functions are known as blocking callbacks. Non-blocking callbacks are those callbacks that do not block the execution of other callbacks.

13) Provide some standard methods of buffer in nodejs.
Ans

  • Compare two buffers:

    We can compare more than one buffer using the buffer.compare(buf1,buf2) function.

  • Check isBuffer:

    In order to know if the given object is a buffer or not we use the buffer.isBuffer(object) function.

  • Check isEncoding:

    If we want to check encoding we use isEncoding function. It returns true if the encoding is a valid encoding.

14) Describe the various stream types?
Ans.

1. Readable stream: Used for receiving and reading the data in an ordered manner.
2. Writable stream: Used for sending data in an ordered manner.
3. Duplex stream: We can send in and receive data together.
4. Transform stream: Is used to make modification in the data. It works in duplex mode.

15) Why should we use a multichannel notification channel for sending email and at the same time why we should avoid it?
Ans. Why to use it:

  • Easy to integrate with node js application.
  • Even non-technical people can edit the content.
  • Third party will maintain the email system

The main reason for avoiding it is that we have to rely on a third-party for sending mails.

Summary:

This article covered some of the most frequently asked nodejs questions Hope you have liked the node js interview questions. Do check out the other nodejs quiz and interview questions to test your knowledge.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

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