InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 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. |
|
| 2. |
What is the purpose of console object? |
|
Answer» console object is used to Used to print information on stdout and stderr. |
|
| 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). |
|
| 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. |
|
| 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. |
|
| 6. |
What is the purpose of __dirname variable? |
|
Answer» The __dirname represents the name of the directory that the currently executing script resides in. |
|
| 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. |
|
| 8. |
How will you read a directory? |
|
Answer» Following is the syntax of the method to read a directory: fs.readdir(path, callback) ParametersHere is the description of the parameters used:
|
|
| 9. |
How will you delete a directory? |
|
Answer» Following is the syntax of the method to remove a directory: fs.rmdir(path, callback) ParametersHere is the description of the parameters used:
|
|
| 10. |
How will you create a directory? |
|
Answer» Following is the syntax of the method to create a directory: fs.mkdir(path[, mode], callback) ParametersHere is the description of the parameters used:
|
|
| 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) ParametersHere is the description of the parameters used:
|
|
| 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) ParametersHere is the description of the parameters used:
|
|
| 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) ParametersHere is the description of the parameters used:
|
|
| 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) ParametersHere is the description of the parameters used:
|
|
| 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. ParametersHere is the description of the parameters used:
|
|
| 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. ParametersHere is the description of the parameters used −
|
|
| 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) ParametersHere is the description of the parameters used:
|
|
| 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. |
|
| 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:
|
|
| 20. |
How many types of streams are present in Node. |
|
Answer» In Node.js, there are four types of streams.
|
|
| 21. |
Name some of the flags used in read/write operation on files. |
|
Answer» flags for read/write operations are following:
|
|
| 22. |
What is difference between synchronous and asynchronous method of fs module? |
|
Answer» Every method in fs module have synchronous as well as asynchronous form. Asynchronous methods takes a last parameter as completion function callback and first parameter of the callback function is error. It is preferred to use asynchronous method instead of synchronous method as former never block the program execution where the latter one does. |
|
| 23. |
fs module provides both synchronous as well as asynchronous methods. |
|
Answer» true. |
|
| 24. |
Which module is used for web based operations? |
|
Answer» http module is used for web based operations. var http = require("http")
|
|
| 25. |
Which module is used for buffer based operations? |
|
Answer» buffer module is used for buffer based operations. var buffer = require("buffer")
|
|
| 26. |
Which module is used for file based operations? |
|
Answer» fs module is used for file based operations. var fs = require("fs")
|
|
| 27. |
What is Piping in Node? |
|
Answer» 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. |
|
| 28. |
What is purpose of Buffer class in Node? |
|
Answer» 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. |
|
| 29. |
What is Event Emmitter? |
|
Answer» EventEmitter class lies in events module. It is accessibly via following syntax − //import events modulevar events = require('events');//create an eventEmitter objectvar eventEmitter = new events.EventEmitter();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. |
|
| 30. |
What is Event Loop? |
|
Answer» 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. |
|
| 31. |
How Node prevents blocking code? |
|
Answer» By providing callback function. Callback function gets called whenever corresponding event triggered. |
|
| 32. |
What is a blocking code? |
|
Answer» 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. |
|
| 33. |
How to update a dependency using npm? |
|
Answer» Update package.json and change the version of the dependency which to be updated and run the following command. C:\Nodejs_WorkSpace>npm update |
|
| 34. |
How to uninstall a dependency using npm? |
|
Answer» Use following command to uninstall a module. C:\Nodejs_WorkSpace>npm uninstall dependency-name |
|
| 35. |
Name some of the attributes of package.json? |
|
Answer» Following are the attributes of Package.json
|
|
| 36. |
How to check the already installed dependencies which are globally installed using npm? |
|
Answer» Use the following command − C:\Nodejs_WorkSpace>npm ls -g |
|
| 37. |
What is local installation of dependencies? |
|
Answer» 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 install express |
|
| 38. |
What is global installation of dependencies? |
|
Answer» Globally installed packages/dependencies are stored in <user-directory>/npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. To install a Node project globally use -g flag. C:\Nodejs_WorkSpace>npm install express -g |
|
| 39. |
What is npm? |
|
Answer» npm stands for Node Package Manager. npm provides following two main functionalities:
|
|
| 40. |
What is the use of Underscore variable in REPL? |
|
Answer» Use _ to get the last result. C:\Nodejs_WorkSpace>node> var x = 10undefined> var y = 20undefined> x + y30> var sum = _undefined> console.log(sum)30undefined> |
|
| 41. |
What is the difference of using var and not using var in REPL while dealing with variables? |
|
Answer» 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. |
|
| 42. |
Can we evaluate simple expression using Node REPL |
|
Answer» Yes. |
|
| 43. |
What is REPL in context of Node? |
|
Answer» 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.
|
|
| 44. |
Is Node a single threaded application? |
|
Answer» Yes! Node uses a single threaded model with event looping. |
|
| 45. |
Is it free to use Node.js? |
|
Answer» Yes! Node.js is released under the MIT license and is free to use. |
|
| 46. |
What are the benefits of using Node.js? |
|
Answer» Following are main benefits of using Node.js
|
|
| 47. |
What do you mean by Asynchronous API? |
|
Answer» All APIs of Node.js library are asynchronous 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. |
|