- A web browser
- A server-side JavaScript runtime environment
- A programming language
- A database management system
-
npm install node
-
npm install -g node
-
npm install -global node
-
npm install --globally node
- npm
- pip
- composer
- yarn
- Using the
add
statement - Using the
require
function - Using the
include
directive - Using the
import
directive
- http
- fs
- path
- url
- To store project documentation
- To manage project dependencies
- To define project routes
- To configure the web server
- Using callbacks
- Using promises
- Using async/await
- All of the above
- http
- fs
- path
- url
- npm
- yarn
- pip
- composer
- To import modules
- To export functions or objects from a module
- To define global variables
- To handle HTTP requests
- Using try/catch blocks
- Using error handling middleware
- Using the
throw
statement - All of the options
- 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.
-
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
- 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.
- 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.
- 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
- 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()
.
- The arguments passed to the Node.js process
- The arguments passed to the Node.js script
- All of the above
- Using the
process.env
object - Using the
process.env()
method - Using the
process.getEnv()
method - All of the above
- To read and write files
- To handle HTTP requests
- To handle binary data
- To handle errors
- True
- False
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.
Which of these are types of streams in Node.js?
- Readable
- Writable
- Functional
- Transform
- 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.
- To import modules
- To define global variables
- To export functions or objects from a module
- To handle HTTP requests
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 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
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.
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