NodeJS interview questions

NodeJS quiz questions

  • 1.

    What is the purpose of process object?

    Answer:

    process object is used to get information on current process. Provides multiple events related to process activities.

    View
  • 2.

    What is the purpose of console object?

    Answer:

    console object is used to Used to print information on stdout and stderr.

    View
  • 3.

    What is the purpose of setInterval function?

    Answer:

    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).

    View
  • 4.

    What is the purpose of clearTimeout function?

    Answer:

    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.

    View
  • 5.

    What is the purpose of setTimeout function?

    Answer:

    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.

    View
  • 6.

    What is the purpose of __dirname variable?

    Answer:

    The __dirname represents the name of the directory that the currently executing script resides in.

    View
  • 7.

    What is the purpose of __filename variable?

    Answer:

    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.

    View
  • 8.

    How will you read a directory?

    Answer:

    Following is the syntax of the method to read a directory:

    fs.readdir(path, callback)

    Parameters

    Here is the description of the parameters used:

    • path - This is the directory name including path.

    • 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 '..'.

    View
  • 9.

    How will you delete a directory?

    Answer:

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

    fs.rmdir(path, callback)

    Parameters

    Here is the description of the parameters used:

    • path - This is the directory name including path.

    • callback - This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

    View
  • 10.

    How will you create a directory?

    Answer:

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

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

    Parameters

    Here is the description of the parameters used:

    • path - This is the directory name including path.

    • mode - This is the directory permission to be set. Defaults to 0777.

    • callback - This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

    View
  • 11.

    How will you delete a file using Node?

    Answer:

    Following is the syntax of the method to delete a file:

    fs.unlink(path, callback)

    Parameters

    Here is the description of the parameters used:

    • path - This is the file name including path.

    • callback - This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

    View
  • 12.

    How will you truncate a file using Node?

    Answer:

    Following is the syntax of the method to truncate an opened file:

    fs.ftruncate(fd, len, callback)

    Parameters

    Here is the description of the parameters used:

    • fd - This is the file descriptor returned by file fs.open() method.

    • 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.

    View
  • 13.

    How will you get information about a file using Node?

    Answer:

    Following is the syntax of the method to get the information about a file:

    fs.stat(path, callback)

    Parameters

    Here is the description of the parameters used:

    • path - This is string having file name including path.

    • 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.

    View
  • 14.

    How will you close a file using Node?

    Answer:

    Following is the syntax of one of the methods to close an opened file:

    fs.close(fd, callback)

    Parameters

    Here is the description of the parameters used:

    • fd - This is the file descriptor returned by file fs.open() method.

    • callback - This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

    View
  • 15.

    How will you write a file using Node?

    Answer:

    Following is the syntax of one of the methods to write into a file:

    fs.writeFile(filename, data[, options], callback)

    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

    Here is the description of the parameters used:

    • path - This is string having file name including path.

    • data - This is the String or Buffer to be written into the file.

    • options - The third parameter is an object which will hold {encoding, mode, flag}. By default encoding is utf8, mode is octal value 0666 and flag is 'w'

    • callback - This is the callback function which gets a single parameter err and used to to return error in case of any writing error.

    View
  • 16.

    How will you read a file using Node?

    Answer:

    Following is the syntax of one of the methods to read from a file:

    fs.read(fd, buffer, offset, length, position, callback)

    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

    Here is the description of the parameters used:

    • fd - This is the file descriptor returned by file fs.open() method.

    • buffer - This is the buffer that the data will be written to.

    • offset - This is the offset in the buffer to start writing at.

    • length - This is an integer specifying the number of bytes to read.

    • position - This is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.

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

    View
  • 17.

    How will you open a file using Node?

    Answer:

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

    fs.open(path, flags[, mode], callback)

    Parameters

    Here is the description of the parameters used:

    • path - This is string having file name including path.

    • flags - Flag tells the behavior of the file to be opened. All possible values have been mentioned below.

    • mode - This sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writeable.

    • callback - This is the callback function which gets two arguments (err, fd).

    View
  • 18.

    What is Chaining in Node?

    Answer:

    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.

    View
  • 19.

    Name some of the events fired by streams.

    Answer:

    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

    View
  • 20.

    How many types of streams are present in Node.

    Answer:

    In Node.js, there are four types of streams.

    • Readable - Stream which is used for read operation.

    • Writable - Stream which is used for write operation.

    • Duplex - Stream which can be used for both read and write operation.

    • Transform - A type of duplex stream where the output is computed based on input.

    View

© 2017 QuizBucket.org