Node.js Test Answers 2019


Share Node.js Test Answers

Posted on June 7, 2019


104 Test Questions:

1. Which of the following will synchronously check if a file/directory exists?

• fs.exists()
• fs.existsSync()
• fs.checkFileSync()
• fs.checkDirSync()

2. Which of the following console commands will update all installed global packages to the latest available versions?

• npm upgrade –g
• npm install –uga
• npm update –g
• npm version —install-latest

3. What does process.argv[1] contain?

• node
• The file path of the JavaScript file.
• The first command line argument.
• The second command line argument.

4. Which of the following command-line arguments to «npm install» will allow an NPM package’s binaries to be run outside the project folder?

• –g
• –global
• –l
• –link

5. Which of the following can be used to access the environment variable, «ENV_VARIABLE» in Node.js?

• process.env.ENV_VARIABLE
• process.argv.ENV_VARIABLE
• process.env.var.ENV_VARIABLE
• process.environment.ENV_VARIABLE

6. Which of the following will copy a file in Node.js?

• var fs = require(‘fs’); fs.createReadStream(‘test.file’).pipe(fs.createWriteStream(‘newFile.file’));
• var fs = require(‘fs’); fs.createReadBuffer(‘test.file’).pipe(fs.createWriteBuffer(‘newFile.file’));
• var fs = require(‘file’); fs.createFileReader(‘test.file’).pipe(fs.createFileWriter(‘newFile.file’));
• var fs = require(‘fs’); fs.createReadBuffer(‘test.file’).stream(fs.createWriteBuffer(‘newFile.file’));

7. Which of the following methods can be used to read the contents of a directory?

• fs.readdir()
• fs.readdirSync()
• fs.readDirectory()
• fs.readdirAsync()

8. Which of the following statements is true about the console Object in Node.js?

• «console.log» can take only a single argument.
• «console.log» prints to stdout without a newline.
• Its functions are synchronous when the destination is a terminal or a file, and asynchronous when it’s a pipe.
• Its functions are asynchronous when the destination is a terminal or a file, and synchronous when it’s a pipe.

9. Which of the following statements is true about the process object in Node.js?

• It is a local object.
• It is an instance of the events.EventEmitter class.
• The process.exit(1) method ends the process with a «success» code.
• «process.stderr» and «process.stdout» are non-blocking if they refer to regular files or TTY file descriptors.

10. Which of the following NPM commands will install both dependencies and devDependencies of a given project?

• npm instal
• npm install –dev
• npm install –production
• None of these

11. What does the following command do? npm view <package-name> version

• It shows the version of the package installed globally.
• It shows the version of the package installed locally.
• It shows the version of the package that is cached.
• It shows the latest version of the package that is available.

12. Which of the following methods will print to the console without a trailing new line?

• process.stdout.print()
• console.error()
• console.log()
• process.stdout.write()

13. Which of the following statements are true about the module.exports object in Node.js?

• It is the object that gets returned from a require() call.
• It can be assigned in a callback.
• Assigning an export object to module.exports will rebind the local exports variable.
• None of these.

14. Which array contains the command line arguments in Node.js?

• process.argv
• args.argv
• arguments.argv
• env.argv

15. Which of the following can be used to get the currently running script’s path in Node.js?

• __filename
• os.tmpdir()
• path.dirname()
• path.basename()

16. Which of the following methods can be used to write a file in Node.js?

• fs.write()
• fs.writeFile()
• fs.createWriteStream()
• fs.writeStream()

17. What does the following code do?

 var http = require(‘http’);

 var fs = require(‘fs’);

file = fs.createWriteStream(«file.png»);
var request = http.get(«http://path/to/file.png», function(response) {     response.pipe(file); });

• It creates an HTTP GET request and pipes its response into a writeable file stream.
• It creates an HTTP GET request, and synchronously pipes its response into a writeable file stream.
• It creates an HTTP POST request and pipes its response into a readable file stream.
• It creates an HTTP POST request and pipes its response into a writeable file stream.

18. Which of the following statements are true about the child_process module in Node.js?

• It is not possible to stream data through a child process’ stdin, stdout, and stderr in a fully non-blocking way.
• Child processes always have two streams associated with them.
• «require(‘child_process’).spawn()» can be used to create a child process.
• «require(‘child_process’).fork()» can be used to create a child process.

19. Which of the following will open a file, then read its contents one line at a time?

• fs.readFileStream()
• fs.readFile()
• fs.createReadStream()
• fs.createFileStream()

20. Which Node.js module can be used to get the IP address of the server the program is running on?

• util
• os
• dns
• net

21. You are handling POST request to the server. Which express middleware gives access to the submitted form data?

• bodyParser
• formHandler
• basicForms
• postHandler

22. Which of the following statement is valid ?

• A. The exec() method runs a command in a shell and buffers the output.
• B. The exec() method returns a buffer with a max size.
• C. The exec() method waits for the process to end and tries to return all the buffered data at once.
• D. All of the above
• E. None of the above

23. Which of the following framework includes input validation?

• Koa
• Hapi.js
• Express
• MEAN

24. In express application you are handling the route ‘/user/:name’. How could you access the ‘name’ property?

• req.params.name
• req.prop.name
• req.query.name
• req.path.name

25. Which of the following names is second param of res.render method?

• locals
• callback
• maxAge
• cacheControl

26. Which of the following code is valid for printing current directory?

• A. console.log(‘Current dir: ‘ + process.cwd());
• B. console.log(‘Current dir: ‘ + console.cwd());
• C. console.log(‘Current dir: ‘ + process.currenWorkingDirectory());
• D. None of the above
• E. All of the above

27. Which of the following is package manager for Node?

• Node
• NPM
• None of above

28. Which of the following template engines is working with Express.js?

• HAML
• EJS
• Handlebars
• All of these

29. Which of the correct way for Callback in NodeJS?

• Callback is an Asynchronous equivalent for a function.
• Callback function is called before the completion of a given task
• Callback function is called at the completion of a given task
• All of above

30. Which of the following statement is true ?

• A. fs.close(fd, callback)
• B. fs.closeFile(fd, callback)
• C. fs.closePath(fd, callback)
• D. None of the above
• E. All of the above

31. Which of the following statement is valid ?

• A. package.json is present in the root directory of any Node application/module.
• B. package.json is used to define the properties of a package.
• C. package.json can be used to update dependencies of a Node application.
• D. A and B both are true
• E. All of the above

32. Which of the following command will show all the modules installed locally?

• $ npm ls -g
• node ls -g
• npm ls
• node ls

33. Which of the following statement is valid?

• A. Node implements File I/O using simple wrappers around standard POSIX functions.
• B. Node File System (fs) module should be imported for File I/O opearations.
• C. Both of the above
• D. None of the above

34. Which of the following is the correct Connect middleware function signature?

• function(req, res, next)
• function(req, res)
• function()
• function(err, req, res)

35. Which of the following methods to checks modifications in files?

• watch
• watchDir
• watchFilesList
• All of these

36. Which of the following framewroks is most popular Node.js framework?

• Koa
• Express
• Hapi
• Total.js

37. Node.js should not be used in the __ area?

• A. Single Page Applications
• B. JSON APIs based Applications
• C. CPU intensive applications
• D. Data Intensive Realtime Applications (DIRT)

38. Which of the following modes supports fs.open method?
Note: There may be more than one right answer.

• rs
• ks
• a+
• ax

39. Would you like to use session storage in your application and which one would be the right way to do it?

• var app = require(‘express’)();
app.use(express.cookieParser());
app.use(express.session({secret: ‘1234567890QWERTY’}));
app.use(function(req,res,next) {
req.session.data = ‘Critical info’;
next();
}
• var express = require(‘express’);
var app = express();
app.use(express.session({secret: ‘1234567890QWERTY’}));
app.use(function(req,res,next) {
req.session.data = ‘Critical info’;
next();
}
• var app = require(‘express’)();
app.use(express.cookieParser());
app.use(express.session({secret: ‘1234567890QWERTY’}));
app.get(‘/’, function(req,res) {
req.session = ‘Critical info’;
res.send(‘session data set’);
}
• var express = require(‘express’);
var app = express();
store = new express.session.MemoryStore;
app.use(express.session({ secret: ‘1234567890QWERTY’, store: store }));
app.get(‘/’, function(req,res) {
req.session = ‘Critical info’;
res.send(‘session data set’);
}

40. Which of the following libraries using to mocking modules?
Note: There may be more than one right answer.

• Rewire
• Mockery
• Stub
• All of these

41. Which of the following command creates a server ?

• A. net.createServer([options][, connectionListener])
• B. net.connect(options[, connectionListener])
• C. net.createConnection(port[, host][, connectListener])
• D. None of the above

42. Which of the following module is required for path specific questions?

• fs module
• os module
• path module
• None of the above

43. Which of the following routing methods available in NodeJS?
Note: There may be more than one right answer.

• header
• get
• post
• delete

44. Why Underscore Variable is used in REPL session?

• A. to get the last command used.
• B. to get the last result.
• C. to store the result.
• D. None of the above

45. Which of the following classes is the parent for repl.REPLServer class?

• REPL.Interface
• readline.Interfase
• REPL
• REPLSERVER

46. For Linux, Which of the following environment variable need to set for successfully NodeJS installation?

• export PATH=$PATH:/usr/local/nodejs/bin
• export NODE=$PATH:/usr/local/nodejs/bin
• None of above

47. Which of the following statement is correct ?

• A. The __dirname represents the name of the directory that the currently executing script resides in.
• B. The __dirname represents the resolved absolute path of code file.
• C. Both of the above
• D. None of the above

48. Which of the following module is required to create child process?

• exec
• spawn
• child_process
• None of the above

49. Which of the following are supported Template engine?
Note: There may be more than one right answer.

• abd
• atpl
• jade
• haml
• hamlet
• All of the above

50. Which of the following is operates asynchronous logic?
Note: There may be more than one right answer.

• Callbacks
• Loops
• Event Emitters
• All of the above

51. How would you get operating system name?

• os.type();
• os.arch();
• os.platform();
• os.name();

52. Which of the following implements stream.Readable interface?
Note: There may be more than one right answer.

• http.IncomingMessage
• fs.ReadStream
• net.Socket
• process.stdin
• process.stdout

53. What is the use of Underscore variable in REPL session?

• To store the result
• To get the last command used
• To get the last result
• None of the above

54. Which of the following classes exposes raw memory allocation outside the V8 heap?

• Buffer
• Stream
• Cluster
• Cache

55. Which array contains the command line arguments in Node.js?

• process.argv
• args.argv
• arguments.argv
• env.argv

56. Which of the following flags working with npm?
Note: There may be more than one right answer.

• -O
• -P
• -R
• -S

57. Which of the following is the result of module.id ?

• file name
• 3-byte machine identifier
• 2-byte process id
• 3-byte process id

58. Which of the following keywords is using to achieve await-like behaviour?

• yield
• yields
• async
• async-await

59. Which of the following will open a file, then read its contents one line at a time?

• fs.readFileStream()
• fs.readFile()
• fs.createReadStream()
• fs.createFileStream()

60. Which of the following statement is valid?

• A. The fork() method method is a special case of the spawn() to create Node processes.
• B. The fork method returns object with a built-in communication channel in addition to having all the methods in a normal Child Process instance.
• C. Both of the above
• D. None of the above

61. Consider the following contents of cube.js , How could you get the volume of a cube utilising this module?

module.exports = function(a) {
return {
volume: function() {
return a * a * a;
}
};
}

• var cube = require(‘./cube.js’);
var a = cube(2);
var volume = a.volume();
• var cube = require(‘./cube.js’);
var a = cube();
var volume = a.volume(2);
• var cube = require(‘./cube.js’);
var volume = cube(2);
• var cube = require(‘./cube.js’);
var a = cube(2);
var volume = a.volume;

62. Which of the following variables you can use in modules?
Note: There may be more than one right answer.

• __filename
• __directory
• __dirname
• __filesize

63. Let’s say you have a file square.js with the following contents, Which are the appropriate ways to use the area function from within other module?

exports.area = function (a) {
return a*a;
}

• var square = require(‘./square.js’);
var sqArea = square.area(3);
• var square = require(‘./square.js’);
var sqArea = square(3);
• var sqArea = require(‘./square.js’).area(3);
• var square = module(‘./square.js’);
var sqArea = square.area(3);

64. Which of the following file extensions supports Node Module System ?

• .js
• .json
• .node
• All of these

65. Which of the following statement is correct while using a Node module http in a Node based application?

• A. var http = import(«http»);
• B. package http;
• C. import http;
• D. var http = require(«http»);

66. You have a file rect.js with the following contents, What would be the appropriate ways to call the exported function?

module.exports = function area(a, b) {
return a*b;
}

• var rect = require(‘./rect.js);
var area = rect(3);
• var rect = require(‘./rect.js);
var area = rect.area(3);
• var area = require(‘./rect.js’)(3);
• var area = require(‘./rect.js’).area(3);

67. Which of the following utility modules available in NodeJS?

• OS
• path
• net
• DNS
• All of above

68. Which are the following debug command to debug node server?
Note: There may be more than one right answer.

• node debug <test.js>
• npm debug <test.js>
• node debug -p <pid>
• node debug <URI>
• All of the above

69. Which of the following is correct ?

• A. Chanining is a mechanism to connect output of one stream to another stream and create a chain of multiple stream operations.
• B. Chanining is normally used with piping operations.
• C. Both of the above
• D. None of the above

70. What REST stands for?

• Representational State Transfer
• Resource Efficient State Transfer
• Real Elegant State Transfer
• Resource Elegant State Transfer

71. Which of the following statement is true about the fs module of Node?

• Asynchronous methods of fs module take last parameter as completion function callback
• Every method in fs module have synchronous as well as asynchronous form
• Both of the above
• None of the above

72. How would you start node with debugger?

• node debug myscript.js
• node myscript.js —debug
• node myscript.js —debugger
• node myscript.js

73. Which of the following events are emitted by the Readable Stream?
Note: There may be more than one right answer.

• ‘data’
• ‘end’
• ‘error’
• ‘response’
• ‘drain’

74. What is default action for the case when we don’t catch the exception and it bubbles all the way back to the event loop?

• print a stack trace and exit
• print a stack trace and continue execution
• print an exception message and restart the program
• exit program immediately

75. Which command enables you to input data using keyboard into your node application?

• process.stdin.resume();
• process.stdin.pause();
• console.read();
• console.readln();

76. APIs of Node.JS are __?

• A. Asynchronous
• B. Synchronous
• C. Both of the above
• D. None of the above

77. Which of the following values can return async functions?

• function
• object
• string
• Non of these

78. Which of the following values is correct about how many lines defaults can be in NODE_REPL_HISTORY_SIZE?

• 1000
• 1024
• 2000
• 2048

79. Which of the following statement is valid?

• A. on property is used to fire event.
• B. on property is used to bind a function with the event.
• C. on property is used to locate an event handler.
• D. None of the above

80. Which of the following code print the name of operating system?

• console.log(‘type : ‘ + os.type);
• console.log(‘type : ‘ + os.getType());
• console.log(‘type : ‘ + os.type());
• None of the above

81. How can you stop reading from stdin?

• process.stdin.pause();
• process.stdin.end();
• process.stdin.close();
• process.stdin.stop();

82. Which of the following will remove a file from your file system?

• var cb = function(err) { if (err) throw err; console.log(‘success deleting file’); }
require(‘fs’).remove(‘filename’, cb);
• var cb = function(err) { if (err) throw err; console.log(‘success deleting file’); }
require(‘fs’).rm(‘filename’, cb);
• var cb = function(err) { if (err) throw err; console.log(‘success deleting file’); }
require(‘fs’).unlink(‘filename’, cb);
• require(‘fs’).removeSync(‘filename’, cb);
console.log(‘success deleting file’);

83. Which of the following engine built of NodeJS framework/platform?

• Firefox’s JavaScript V8 Engine
• Google Chrome’s JavaScript V8 Engine
• None of above

84. Which of the following should not to use in NodeJS?

• I/O bound Applications
• CPU Intensive Applications
• Data Streaming Applications
• Data Intensive Real-time Applications

85. Which of the following events would be fired in http module when a new TCP stream is established?

• ‘connection’
• ‘request’
• ‘stream’
• ‘connect’

86. Which of the following statement is correct ?

• A. Webservices based on REST Architecture are known as RESTful web services.
• B. Webservices uses HTTP methods to implement the concept of REST architecture.
• C. Both of the above
• D. None of the above

87. Route path: /train/from-:to
Request URL: <a href=»http://localhost:3000/train/AHM-BRD»>http://localhost:3000/train/AHM-BRD</a>
What should be request params?

• { «from»: «AHM», «to»: «BRD» }
• { «train»: «AHM», «to»: «BRD» }
• { «from»: «AHM», «train»: «BRD» }
• None of the above

88. What happens when executing the following code?
assert = require(‘assert’);
assert(false, ‘assert false’);

• assert throws an AssertionError, program execution stops
• assert throws an AssertionError, program execution continues
• ‘assert false’ is printed to console, program execution continues
• assertion fails, ‘assert false’ is printed to console, program execution stops

89. How would you get the filename of the code being executed?

• __filename
• __dirname + __filename
• module.__filename
• module.__dirname + module.__filename

90. Which of the following ways to create a child process?

• exec() method
• spawn() method
• fork() method
• All of the above
• None of the above

91. Which of the following libs provides basic HTTP authentication for protecting data?

• auth-base
• basicAuth
• connect
• http

92. Which of the following libs using for debugging node-applications?

• npm-debug
• node-debugger
• node-inspector
• debugger

93. NodeJS includes a full-featured out-of-process debugging utility accessible via a which protocol?

• TCP-based protocol
• UDP-based protocol
• All of the above

94. How can you check if two arrays (a1 and a2) are equal element by element using ‘assert’ module?

• assert.deepEqual(a1, a2)
• assert.strictEqual(a1, a2)
• assert.arrayEqual(a1, a2)
• assert(a1 == a2)

95. How to enabled V8 inspector for debug?
Note: There may be more than one right answer.

• —inspect
• —inspect=<PORT>
• —debug=<PORT>
• All of the above

96. Which module of core node.js API is used for writing unit tests for your applications?

• assert
• should
• mocha
• jsUnit

97. Suppose we have this construction in Express.js project , Which of the following paths will match this route?

app.get(/.*moon$/, function(req, res) {
return res.send(‘/.*moon$/’);
});

• moonlight
• moon
• moonbeam
• comoon

98. Which of the following commands exit from REPL?

• ctrl+X
• ESC
• double ctrl+c
• ctrl+c

99. Suppose we have this construction in Expres.js project,Which of the following paths will match this route?

app.get(‘/kl+mn’, function(req, res) {
return res.send(‘kl+mn’);
});

• klmn
• kllmn
• kmcd
• All of these.

100. How would you get the number of CPU’s in your machine?

• require(‘os’).cpus().length;
• process.cpus.length;
• system.cpu.length;
• require(‘os’).cpus();

101. Which of the following files using for setting start point in deployment services?

• .travis.yml
• PROCFILE
• .CONFIG
• .npm-start

102. How would you get the name of the JavaScript file being executed?

• process.argv[1]
• __filename
• process.__filename
• __dirname + __filename

103. Which of the libs are exists in Node Core Libraries?
Note: There may be more than one right answer.

• .Net
• HTTP
• STDLIB
• Path

104. Which statement best describes console functions behavior?

• The console functions are synchronous when the destination is a terminal or a file and asynchronous when it’s a pipe.
• The console functions are always asynchronous.
• The console functions are always synchronous.
• The console functions are always asynchronous, except for when they are called from the child process.