Skip to content

Latest commit

 

History

History
271 lines (183 loc) · 7.01 KB

nodejs.md

File metadata and controls

271 lines (183 loc) · 7.01 KB

Node.JS

What is Node.js?

  • A web browser
  • A server-side JavaScript runtime environment
  • A programming language
  • A database management system

Which command is used to install Node.js globally?

  • npm install node
  • npm install -g node
  • npm install -global node
  • npm install --globally node

Which of these are package managers for Node.js?

  • npm
  • pip
  • composer
  • yarn

How can you include external modules in Node.js?

  • Using the add statement
  • Using the require function
  • Using the include directive
  • Using the import directive

Which module is used to create a web server in Node.js?

  • http
  • fs
  • path
  • url

What is the purpose of the package.json file in a Node.js project?

  • To store project documentation
  • To manage project dependencies
  • To define project routes
  • To configure the web server

How can you handle asynchronous operations in Node.js?

  • Using callbacks
  • Using promises
  • Using async/await
  • All of the above

Which module is used for file system operations in Node.js?

  • http
  • fs
  • path
  • url

What is the default package manager for Node.js?

  • npm
  • yarn
  • pip
  • composer

What is the purpose of the exports object in Node.js?

  • To import modules
  • To export functions or objects from a module
  • To define global variables
  • To handle HTTP requests

How can you handle errors in Node.js?

  • Using try/catch blocks
  • Using error handling middleware
  • Using the throw statement
  • All of the options

What is the purpose of the __dirname variable in Node.js?

  • To get the current working directory
  • To get the directory name of the current module
  • To get the directory name of the main script
  • To get the directory name of the parent module

To get the directory name of the current module. __dirname is a global variable that contains the directory name of the current module.

How can you install a specific version of a package using npm?

  • npm install package-name@version
  • npm install package-name --version
  • npm install package-name -v version
  • npm install package-name --v version

npm install package-name@version

Node.js runs http requests in a single thread?

  • True
  • False

Node.js runs http requests in a single thread. Node.js is single-threaded, but it uses an event loop to handle asynchronous operations.

All I/O operations in Node.js are asynchronous?

  • True
  • False

All I/O operations in Node.js are asynchronous. Node.js is single-threaded, but it uses an event loop to handle asynchronous operations.

What is the purpose of the process object in Node.js?

  • To get information about the current process
  • To get information about the current module
  • To get information about the current script
  • All of the above

What is the best practice of gracefully shutting down a Node.js process?

  • Using the process.exit() method
  • Using the process.kill() method
  • Using the process.on('SIGTERM') event
  • Using the process.on('SIGINT') event

Using the process.on('SIGTERM') event. The SIGTERM event is emitted when the process is terminated. It is a good practice to gracefully shut down a Node.js process by listening for this event and then calling process.exit().

What is the value of the process.argv property in Node.js?

  • The arguments passed to the Node.js process
  • The arguments passed to the Node.js script
  • All of the above

How do you read environment variables in Node.js?

  • Using the process.env object
  • Using the process.env() method
  • Using the process.getEnv() method
  • All of the above

What is the purpose of the Buffer class in Node.js?

  • To read and write files
  • To handle HTTP requests
  • To handle binary data
  • To handle errors

Node.js is a single-threaded runtime environment?

  • True
  • False

Node.js Streams

What is the purpose of the pipe method in Node.js streams?

  • To read data from a stream
  • To write data to a stream
  • To read data from a readable stream and write it to a writable stream
  • To close a stream

To read data from a readable stream and write it to a writable stream. The pipe method is used to read data from a readable stream and write it to a writable stream.

Node.js Streams Types

Which of these are types of streams in Node.js?

  • Readable
  • Writable
  • Functional
  • Transform

When does setTimeout execute the callback function?

  • After the specified number of milliseconds
  • Before the specified number of milliseconds
  • After the specified number of milliseconds, but not exactly
  • Before the specified number of milliseconds, but not exactly

setTimeout executes the callback function after the specified number of milliseconds, but because of the event loop, it is not guaranteed to be executed exactly after the specified number of milliseconds.

What is the purpose of the module.exports object in Node.js?

  • To import modules
  • To define global variables
  • To export functions or objects from a module
  • To handle HTTP requests

Requiring modules

Here is module foo.js:

consol.log('Hello from foo.js');

And here is module index.js requiring foo.js:

require('./foo');
require('./foo');

What will be the output of node index.js?

  • Hello from foo.js
  • Hello from foo.js Hello from foo.js
  • Nothing is printed to the console

Node.js caches modules on first require. So the output will be Hello from foo.js only once.

When is child_process.exec() preferable to child_process.spawn()?

  • When you want to execute a command in a new process
  • When you want to execute a command in a new thread
  • When you want to execute a command in a shell
  • When you want to execute a command in a new terminal

Error handling

Why is the following code not a good practice in error handling?

process.on('uncaughtException', (err) => {
	console.log('Caught exception: ' + err);
});
  • Because it will catch all exceptions
  • Because it will catch all unhandled exceptions but the process will continue to run
  • Because it will catch all unhandled exceptions and the process will exit
  • Because it will catch all unhandled exceptions and the process will restart

process.on('uncaughtException') is used to catch unhandled exceptions. However, the process will continue to run after the exception is caught. This is not a good practice because the process may be in an inconsistent state.

Errors and promises

What is the output of the following code?

const p = new Promise(function (resolve, reject) {
	reject(new Error('Oops'));
});

p.catch(function (err) {
	console.log(err.message);
});
  • Error: Oops
  • Oops
  • Error
  • undefined