Node JS Interview Questions and Answers Set 8

71. How Will You Truncate A File Using Node?

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.

72. What Is The Purpose Of Process Object?

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

73. What is the difference between events and callbacks in Node.js?

Although, Events and Callbacks look similar the differences lies in the fact that callback functions are called when an asynchronous function returns its result whereas event handling works on the observer pattern. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through the events module and EventEmitter class which is used to bind events and event listeners.

74. Explain the tasks of terms used in Node REPL.

Following are the terms used in REPL with their defined tasks:

Read: It reads user’s input; parse the input into JavaScript data-structure and stores in memory.

Eval: It takes and evaluates the data structure.

Print: It is used to print the result.

Loop: It loops the above command until user press ctrl-c twice to terminate.

75. What is the Punycode in Node.js?

The Punycode is an encoding syntax which is used to convert Unicode (UTF-8) string of characters to ASCII string of characters. It is bundled with Node.js v0.6.2 and later versions. If you want to use it with other Node.js versions, then use npm to install Punycode module first. You have to used require (‘Punycode’) to access it.

Syntax:

punycode = require(‘punycode’);

NODE JS & CERTIFICATION
Weekend / Weekday Batch

76. What Is The Purpose Of Cleartimeout Function?

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.

77. How Will You Delete A Directory?

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.

78. Explain “Path” module in Node.JS?

“Path” module will be used for transforming and handling file paths. Below is the syntax of path module –

var mypath = require(“path”)

79. Explain FS module ?

Here FS stands for “File System” and fs module is used for File I/O. FS module can be imported in the following way

var test = require(“fs”)

80. Explain – “console.log([data][, …])” statement in Node.JS?

This statement is used for printing to “stdout” with newline and this function takes multiple arguments as “printf()”.