Node JS Online Test

Welcome to Node JS Online Test! This test consists of 25 multiple-choice questions (MCQs) to test your knowledge of essential Node.js topics such as asynchronous programming, file system operations, modules, Express.js, and error handling. 

Whether you are a beginner eager to test your understanding or an experienced developer looking to confirm your command of Node.js, this test thoroughly assesses your technical abilities. 

1. Which of the following core modules is used to create a web server in Node.js?

a) http
b) url
c) fs
d) querystring

2. What does the fs module in Node.js handle?

a) File system operations
b) Networking
c) Encoding conversion
d) Data streaming

3. How do you read a file asynchronously in Node.js?

fs.readFile('/path/to/file', 'utf8', callback);
a) fs.readFile('/path/to/file', 'utf8', callback);
b) fs.readFileSync('/path/to/file', 'utf8');
c) fs.getFile('/path/to/file', 'utf8', callback);
d) fs.openFile('/path/to/file', 'utf8', callback);

4. What is process.env in Node.js?

a) A global object that provides information about, and control over, the current Node.js process.
b) A module to configure environment variables.
c) A method to exit the current process.
d) A module to handle HTTP requests.

5. Which method would you use to write data to the request body in an HTTP server request?

a) req.write(data);
b) res.write(data);
c) req.send(data);
d) res.send(data);

6. How can you handle exceptions globally in a Node.js application?

a) Using try/catch in each function
b) Using a global error handler middleware
c) Listening to the uncaughtException event on the process object
d) Node.js automatically handles all exceptions

7. What does module.exports do in a Node.js module?

a) Imports required modules
b) Sets the initial environment variables
c) Exports modules for use in other parts of the application
d) Starts the server

8. Which Node.js module provides utilities for working with file and directory paths?

a) fs
b) path
c) os
d) net

9. How do you create a simple server in Node.js that returns "Hello, World!" on every request?

const http = require('http');
http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, World!');
a) By using the http module's createServer method
b) By using the express module's send method
c) By using the net module's connect method
d) By using the fs module's writeFile method

10. What is the use of the Buffer class in Node.js?

a) Managing data caching
b) Handling raw binary data
c) Encoding and decoding JSON data
d) Parsing URL parameters

11. What are streams in Node.js?

a) A sequence of data being processed over time
b) A one-time data processing mechanism
c) Data stored in a buffer until it is retrieved
d) A method for managing HTTP request data

12. Which method do you use to read from a stream in Node.js?

b) stream.getData()
c) stream.fetch()
d) stream.write()

13. What is an event emitter in Node.js?

a) A tool for managing synchronous operations
b) A way to perform data validation
c) An implementation for handling events
d) A utility for logging error messages

14. What is a typical use case for the Node.js cluster module?

a) To ensure that data is clustered in the database
b) To allow for a single instance of Node.js to run on multiple CPUs
c) To manage memory usage across user sessions
d) To encrypt and decrypt data securely

15. What is npm in the context of Node.js?

a) A Node.js command-line tool for managing versions
b) The default package manager for the JavaScript runtime environment Node.js
c) A Node.js framework for building web servers
d) A module used to handle HTTP requests and responses

16. How do you install a package using npm?

npm install package-name
a) npm get package-name
b) npm add package-name
c) npm install package-name
d) npm setup package-name

17. What is package.json in a Node.js application?

a) A file that contains configuration data for the operating system
b) A script that runs when the application starts
c) A file that holds various metadata relevant to the project
d) A database file that stores user data

18. How can you update all the dependencies in a Node.js project to their latest version?

a) npm update
b) npm upgrade
c) npm refresh
d) npm renew

19. What does the path.resolve() function do?

a) Converts a relative path into an absolute path
b) Resolves a sequence of paths into a single path
c) Checks if a path exists
d) Both a) and b) are correct

20. What is the difference between exports and module.exports in Node.js?

a) exports is used for importing modules, while module.exports is for exporting them.
b) exports is a shortcut to module.exports but does not return a reference when reassigned.
c) exports and module.exports point to the same object and are interchangeable.
d) module.exports is used for built-in modules, while exports is for user-defined modules.

21. How do you enable the use of ES6 import and export syntax in a Node.js application?

a) Add "type": "module" in the package.json file
b) Use the --es6 flag when starting Node.js
c) Import the es6 module
d) Rewrite Node.js to support ES6

22. Which Node.js module is used to work with HTTP cookies?

a) http
b) url
c) cookie-parser
d) querystring

23. How do you create a secure HTTPS server in Node.js?

const https = require('https');
const fs = require('fs');
const options = {
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
    https.createServer(options, function (req, res) {
        res.end("hello world\n");
a) Use the http module with SSL options
b) Use the https module with key and cert options
c) Use the net module with secure options
d) Use the tls module directly

24. What is the output of this Node.js script?

setImmediate(() => console.log('B'));
process.nextTick(() => console.log('C'));
a) A, D, B, C
b) A, C, D, B
c) A, D, C, B
d) C, A, D, B

25. Which Node.js method is used to run a shell command?

b) console.execute()
c) child_process.exec()
d) os.system()