Wednesday, 15 January 2025

lecture Notes on Class 1: Introduction to Node.js

 

lecture Notes on  Class 1: Introduction to Node.js

Objective

  • Understand the basics of Node.js.
  • Learn about its installation process.
  • Explore its role in web development.

What is Node.js?

Node.js is an open-source, cross-platform runtime environment that allows developers to run JavaScript on the server side. Traditionally, JavaScript was only executed in the browser, but with Node.js, you can now use JavaScript to build backend applications.

Key Features of Node.js:

  1. Asynchronous and Event-Driven: Handles multiple requests simultaneously without blocking the thread.
  2. Fast Execution: Built on Google Chrome's V8 engine, Node.js is optimized for high performance.
  3. Single Programming Language: Enables developers to use JavaScript for both frontend and backend development.
  4. Extensive Library Support: Includes npm (Node Package Manager) for easy integration of third-party libraries.

Uses of Node.js:

  • Building web servers and APIs.
  • Developing real-time applications like chat apps.
  • Creating command-line tools.
  • Developing single-page and progressive web applications.

Installing Node.js and npm

Steps to Install Node.js:

  1. Visit the official Node.js website.
  2. Download the LTS (Long-Term Support) version suitable for your operating system (Windows, macOS, Linux).
  3. Follow the installation instructions specific to your OS.

Verify Installation:

  1. Open your terminal or command prompt.
  2. Run the following commands to check the installed versions:
    • node -v (to check Node.js version)
    • npm -v (to check npm version)

If both commands return version numbers, the installation was successful.


First Node.js Program

Creating a Simple Script

  1. Open a text editor (e.g., VS Code, Notepad++).
  2. Write the following code and save it as app.js:

javascript

CopyEdit

// app.js

console.log("Hello, World!");

Running the Script

  1. Open the terminal or command prompt.
  2. Navigate to the directory where app.js is saved using the cd command.
  3. Execute the script using the command:

bash

CopyEdit

node app.js

You should see the output:

CopyEdit

Hello, World!


Lab Tasks

  1. Install Node.js and npm:
    Follow the steps mentioned in the "Installing Node.js and npm" section.
  2. Verify the Installation:
    Run node -v and npm -v in the terminal to confirm successful installation.
  3. Write and Execute a Script:
    • Create a file named app.js.
    • Write a simple script to print "Hello, World!" using the instructions provided above.
    • Run the script with node app.js.

Conclusion

  • Node.js extends JavaScript to the server side, enabling full-stack development with a single programming language.
  • Installing and verifying Node.js and npm is the first step toward building powerful server-side applications.
  • Running a basic script like "Hello, World!" introduces you to Node.js' environment and execution process.

Next Steps:
In the upcoming classes, we’ll explore how to use Node.js to build web servers and interact with external libraries using npm.

2/2

You said:

write 10 assignments from the above concept

ChatGPT said:

ChatGPT

Assignments on Introduction to Node.js

Assignment 1: Understanding Node.js

  • Research and write a brief essay (300-400 words) on the following:
    1. What is Node.js?
    2. Why is Node.js popular in web development?
    3. List three real-world applications built using Node.js.

Assignment 2: Node.js Installation

  • Install Node.js and npm on your computer.
  • Write down the step-by-step process you followed, including screenshots of:
    1. The download page.
    2. Successful installation.
    3. Verification using node -v and npm -v.

Assignment 3: First Node.js Script

  • Write a Node.js script named hello.js that prints your name, course name, and the message "Welcome to Node.js!"
  • Run the script using the command node hello.js and share the output.

Assignment 4: Exploring Node.js Documentation

  • Visit the official Node.js documentation.
  • Write a summary of the following sections:
    1. About Node.js
    2. Features of Node.js
    3. Commonly used modules (e.g., fs, http).

Assignment 5: Create and Execute a Custom Script

  • Write a Node.js script named info.js that performs the following:
    1. Prints the current date and time.
    2. Prints the name of your operating system.
  • Run the script using Node.js and attach the output.

Assignment 6: Explore npm Commands

  • Use npm to perform the following tasks:
    1. List globally installed npm packages with npm list -g.
    2. Install a simple npm package (e.g., lodash) globally.
    3. Write a summary of the npm install, npm update, and npm uninstall commands.

Assignment 7: Understanding Asynchronous JavaScript

  • Research and explain how Node.js handles asynchronous operations using the event loop.
  • Write a simple asynchronous Node.js script using setTimeout() that prints:
    • "Starting the process..."
    • "This message appears after 3 seconds."

Assignment 8: Create a Node.js Calculator

  • Write a Node.js script calculator.js that performs basic arithmetic operations: addition, subtraction, multiplication, and division.
  • The script should print results for the following inputs:
    • 5 + 10
    • 20 - 5
    • 4 * 6
    • 30 / 5

Assignment 9: Write and Debug a Node.js Script

  • Write a script debug.js with intentional errors (e.g., missing semicolons or incorrect variable names).
  • Debug the errors using the Node.js terminal and share:
    1. The original script with errors.
    2. The corrected script.
    3. Steps you took to fix the errors.

Assignment 10: Exploring Node.js Modules

  • Research the core modules of Node.js (e.g., fs, path, os).
  • Write a script modules.js that performs the following:
    1. Reads and prints the content of a text file using the fs module.
    2. Prints the absolute path of the file using the path module.
    3. Prints the system architecture using the os module.

These assignments will help solidify your understanding of Node.js basics, installation, and its role in web development.

You said:

write the solutions for all

ChatGPT said:

ChatGPT

Solutions for Node.js Assignments


Assignment 1: Understanding Node.js

Solution:
Write a brief essay covering these points:

  1. What is Node.js?
    • Node.js is an open-source, cross-platform runtime environment that allows JavaScript to run on the server side. It uses the V8 engine for fast execution and is designed for building scalable network applications.
  2. Why is Node.js popular in web development?
    • It allows JavaScript to be used both on the frontend and backend.
    • Non-blocking I/O and event-driven architecture make it efficient.
    • It has a rich ecosystem with npm, supporting rapid development.
  3. Examples of real-world applications built using Node.js:
    • Netflix (Streaming Service)
    • LinkedIn (Professional Networking)
    • Uber (Ride-sharing App)

Assignment 2: Node.js Installation

Solution:

  1. Go to Node.js, download the LTS version, and install it.
  2. Verify the installation:

bash

CopyEdit

node -v   # Prints the Node.js version

npm -v    # Prints the npm version

  1. Include screenshots showing the download, installation, and version verification.

Assignment 3: First Node.js Script

Script (hello.js):

javascript

CopyEdit

// hello.js

console.log("Name: John Doe");

console.log("Course: Web Development");

console.log("Welcome to Node.js!");

Command to Run:

bash

CopyEdit

node hello.js

Output:

vbnet

CopyEdit

Name: John Doe

Course: Web Development

Welcome to Node.js!


Assignment 4: Exploring Node.js Documentation

Solution:
Visit Node.js Documentation.
Summary:

  • About Node.js: Node.js is a JavaScript runtime built on Chrome's V8 engine, designed for scalable network applications.
  • Features:
    1. Event-driven architecture.
    2. Non-blocking I/O.
    3. Lightweight and efficient.
  • Common Modules:
    • fs: File system operations.
    • http: Building web servers.
    • os: Access system information.

Assignment 5: Create and Execute a Custom Script

Script (info.js):

javascript

CopyEdit

// info.js

console.log("Current Date and Time:", new Date().toLocaleString());

console.log("Operating System:", process.platform);

Command to Run:

bash

CopyEdit

node info.js

Sample Output:

sql

CopyEdit

Current Date and Time: 2025-01-16 10:00:00

Operating System: win32


Assignment 6: Explore npm Commands

Solution:

  1. List globally installed npm packages:

bash

CopyEdit

npm list -g

  1. Install a package (e.g., lodash):

bash

CopyEdit

npm install -g lodash

  1. Summary of npm commands:
    • npm install: Installs a package.
    • npm update: Updates installed packages.
    • npm uninstall: Removes a package.

Assignment 7: Understanding Asynchronous JavaScript

Script (async.js):

javascript

CopyEdit

// async.js

console.log("Starting the process...");

 

setTimeout(() => {

  console.log("This message appears after 3 seconds.");

}, 3000);

Command to Run:

bash

CopyEdit

node async.js

Output:

arduino

CopyEdit

Starting the process...

(This message appears after 3 seconds.)

This message appears after 3 seconds.


Assignment 8: Create a Node.js Calculator

Script (calculator.js):

javascript

CopyEdit

// calculator.js

console.log("Addition: 5 + 10 =", 5 + 10);

console.log("Subtraction: 20 - 5 =", 20 - 5);

console.log("Multiplication: 4 * 6 =", 4 * 6);

console.log("Division: 30 / 5 =", 30 / 5);

Command to Run:

bash

CopyEdit

node calculator.js

Output:

makefile

CopyEdit

Addition: 5 + 10 = 15

Subtraction: 20 - 5 = 15

Multiplication: 4 * 6 = 24

Division: 30 / 5 = 6


Assignment 9: Write and Debug a Node.js Script

Original Script (debug.js) with Errors:

javascript

CopyEdit

// debug.js

consle.log("Hello, Debugging"); // Typo in console

let num = 5

console.log("Number:", num)

Corrected Script (debug.js):

javascript

CopyEdit

// debug.js

console.log("Hello, Debugging");

let num = 5;

console.log("Number:", num);

Debugging Steps:

  1. Run the script:

bash

CopyEdit

node debug.js

  1. Fix the errors:
    • Corrected consle to console.
    • Added a semicolon after let num = 5.

Output:

javascript

CopyEdit

Hello, Debugging

Number: 5


Assignment 10: Exploring Node.js Modules

Script (modules.js):

javascript

CopyEdit

// modules.js

const fs = require("fs");

const path = require("path");

const os = require("os");

 

// 1. Read and print the content of a file

fs.writeFileSync("sample.txt", "Hello, Node.js!");

const data = fs.readFileSync("sample.txt", "utf8");

console.log("File Content:", data);

 

// 2. Print the absolute path of the file

console.log("File Path:", path.resolve("sample.txt"));

 

// 3. Print the system architecture

console.log("System Architecture:", os.arch());

Command to Run:

bash

CopyEdit

node modules.js

Output:

mathematica

CopyEdit

File Content: Hello, Node.js!

File Path: /absolute/path/to/sample.txt

System Architecture: x64


These solutions provide hands-on practice with Node.js basics, strengthening foundational concepts.

You said:

write 100 MCqs from the above topic

ChatGPT said:

ChatGPT

Here are 100 multiple-choice questions (MCQs) based on the topic "Introduction to Node.js."


Basics of Node.js

  1. What is Node.js primarily used for?
    a) Game development
    b) Machine learning
    c) Server-side scripting
    d) Database management
    Answer: c) Server-side scripting
  2. Which JavaScript engine does Node.js use?
    a) SpiderMonkey
    b) Chakra
    c) V8
    d) JavaScriptCore
    Answer: c) V8
  3. Node.js is built on which programming language?
    a) Python
    b) C++
    c) Java
    d) Ruby
    Answer: b) C++
  4. What kind of architecture does Node.js use?
    a) Thread-based
    b) Event-driven, non-blocking I/O
    c) Blocking I/O
    d) Multi-threaded
    Answer: b) Event-driven, non-blocking I/O
  5. Which of the following is NOT a feature of Node.js?
    a) Asynchronous programming
    b) Event-driven architecture
    c) Blocking I/O
    d) Lightweight
    Answer: c) Blocking I/O

Node.js Installation

  1. Which command is used to check the Node.js version after installation?
    a) node --version
    b) node -v
    c) npm -v
    d) version node
    Answer: b) node -v
  2. What does npm stand for?
    a) Node.js Programming Module
    b) Node Package Manager
    c) Non-blocking Process Module
    d) Node Processing Manager
    Answer: b) Node Package Manager
  3. Which file is required to manage dependencies in a Node.js project?
    a) node_modules.json
    b) app.js
    c) package.json
    d) index.js
    Answer: c) package.json
  4. Where can you download Node.js from?
    a) nodejs.org
    b) npmjs.com
    c) node.com
    d) github.com/node
    Answer: a) nodejs.org
  5. Which command checks the npm version installed?
    a) npm --version
    b) npm -v
    c) both a and b
    d) npm version-check
    Answer: c) both a and b

First Node.js Script

  1. Which command is used to execute a Node.js script?
    a) run script.js
    b) execute script.js
    c) node script.js
    d) start script.js
    Answer: c) node script.js
  2. What will the following script output?

javascript

CopyEdit

console.log("Hello, Node.js!");

a) Nothing
b) Hello, Node.js!
c) Undefined
d) Error
Answer: b) Hello, Node.js!

  1. Which extension is used for Node.js files?
    a) .node
    b) .javascript
    c) .js
    d) .script
    Answer: c) .js
  2. How do you print the current date in Node.js?
    a) console.log(new Date());
    b) print(Date.now());
    c) console.log(date.today());
    d) None of the above
    Answer: a) console.log(new Date());
  3. What will happen if you run an empty Node.js file?
    a) Error
    b) It will exit without output
    c) Prints "undefined"
    d) None of the above
    Answer: b) It will exit without output

Core Features

  1. Which of the following is a core module in Node.js?
    a) express
    b) fs
    c) mongoose
    d) lodash
    Answer: b) fs
  2. What does the fs module in Node.js handle?
    a) File system operations
    b) Networking
    c) Database connections
    d) HTTP requests
    Answer: a) File system operations
  3. Which module is used to create a web server in Node.js?
    a) os
    b) http
    c) path
    d) url
    Answer: b) http
  4. How do you import a core module in Node.js?
    a) import moduleName from "module";
    b) require("moduleName");
    c) include "moduleName";
    d) load("moduleName");
    Answer: b) require("moduleName");
  5. Which module is used to interact with the file system in Node.js?
    a) fs
    b) path
    c) file
    d) os
    Answer: a) fs

npm Basics

  1. What does npm install do?
    a) Installs the Node.js runtime
    b) Installs project dependencies
    c) Updates npm version
    d) Uninstalls dependencies
    Answer: b) Installs project dependencies
  2. How do you install a package globally using npm?
    a) npm install -g packageName
    b) npm install --global packageName
    c) Both a and b
    d) npm add packageName
    Answer: c) Both a and b
  3. What is the purpose of the node_modules folder?
    a) To store global npm packages
    b) To store project-specific npm packages
    c) To store Node.js core modules
    d) To store temporary files
    Answer: b) To store project-specific npm packages
  4. How do you uninstall a package using npm?
    a) npm remove packageName
    b) npm uninstall packageName
    c) npm delete packageName
    d) Both a and b
    Answer: b) npm uninstall packageName
  5. What is the package.json file used for?
    a) Tracking project dependencies
    b) Storing environment variables
    c) Managing file system operations
    d) Storing logs
    Answer: a) Tracking project dependencies

Asynchronous Programming

  1. What is the main advantage of asynchronous programming in Node.js?
    a) Faster execution of synchronous tasks
    b) Efficient handling of multiple requests
    c) Improved code readability
    d) Simplified debugging
    Answer: b) Efficient handling of multiple requests
  2. What function is used to handle delays in Node.js?
    a) setInterval()
    b) setDelay()
    c) setTimeout()
    d) sleep()
    Answer: c) setTimeout()
  3. What does the following code do?

javascript

CopyEdit

setTimeout(() => {

  console.log("Hello after 2 seconds");

}, 2000);

a) Prints "Hello after 2 seconds" immediately
b) Prints "Hello after 2 seconds" after a 2-second delay
c) Throws an error
d) None of the above
Answer: b) Prints "Hello after 2 seconds" after a 2-second delay

  1. What is the event loop in Node.js?
    a) A loop that handles blocking operations
    b) A mechanism for asynchronous execution
    c) A way to create HTTP servers
    d) A built-in database
    Answer: b) A mechanism for asynchronous execution
  2. Which function in Node.js is asynchronous?
    a) fs.readFile()
    b) console.log()
    c) Date.now()
    d) Math.random()
    Answer: a) fs.readFile()

Would you like me to continue with the rest?

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs to complete the 100 questions.


Event-Driven Architecture

  1. What is the purpose of the Node.js event loop?
    a) To execute blocking operations
    b) To handle asynchronous operations
    c) To manage database transactions
    d) To serve static files
    Answer: b) To handle asynchronous operations
  2. In Node.js, events are handled by which module?
    a) events
    b) fs
    c) eventManager
    d) http
    Answer: a) events
  3. Which method is used to listen for an event in Node.js?
    a) on()
    b) listen()
    c) emit()
    d) eventHandler()
    Answer: a) on()
  4. What does the emit() function do in Node.js?
    a) Emits an event
    b) Listens to an event
    c) Stops the event loop
    d) None of the above
    Answer: a) Emits an event
  5. Which of the following best describes an event in Node.js?
    a) A synchronous process
    b) A signal that something has happened
    c) A function to stop asynchronous tasks
    d) None of the above
    Answer: b) A signal that something has happened

REPL (Read-Eval-Print Loop)

  1. What does REPL stand for in Node.js?
    a) Repeat-Evaluate-Print-Loop
    b) Read-Eval-Print-Loop
    c) Run-Execute-Print-Loop
    d) None of the above
    Answer: b) Read-Eval-Print-Loop
  2. Which command starts the Node.js REPL?
    a) node repl
    b) repl start
    c) node
    d) node start
    Answer: c) node
  3. How do you exit the REPL in Node.js?
    a) Press Ctrl + D
    b) Type .exit
    c) Both a and b
    d) Press Ctrl + C
    Answer: c) Both a and b
  4. Which of the following commands will display all the available commands in the REPL?
    a) .commands
    b) help
    c) .help
    d) .list
    Answer: c) .help
  5. What will the REPL output for 2 + 3?
    a) 23
    b) Error
    c) 5
    d) None of the above
    Answer: c) 5

Node.js Modules

  1. What are Node.js modules?
    a) Files containing multiple programming languages
    b) Reusable code libraries
    c) Built-in databases
    d) Functions that stop the server
    Answer: b) Reusable code libraries
  2. Which keyword is used to export a module in Node.js?
    a) import
    b) exports
    c) require
    d) module.exports
    Answer: d) module.exports
  3. How do you import a custom module in Node.js?
    a) require('./moduleName');
    b) import moduleName from './moduleName';
    c) include './moduleName';
    d) load('./moduleName');
    Answer: a) require('./moduleName');
  4. Which module is used to work with URLs in Node.js?
    a) fs
    b) http
    c) url
    d) os
    Answer: c) url
  5. What does the path module handle in Node.js?
    a) File paths and directories
    b) URL parsing
    c) HTTP requests
    d) Database queries
    Answer: a) File paths and directories

Node.js Global Objects

  1. What is the __dirname variable in Node.js?
    a) Current file name
    b) Current working directory
    c) Directory name of the current module
    d) Node.js installation path
    Answer: c) Directory name of the current module
  2. What does process in Node.js represent?
    a) Global module for file operations
    b) Node.js runtime environment
    c) The operating system
    d) None of the above
    Answer: b) Node.js runtime environment
  3. Which of the following is a global object in Node.js?
    a) setTimeout
    b) console
    c) process
    d) All of the above
    Answer: d) All of the above
  4. How do you get the current Node.js version using process?
    a) process.version
    b) process.env.NODE_VERSION
    c) process.nodeVersion
    d) process.getVersion()
    Answer: a) process.version
  5. What will console.log(global) output?
    a) A list of all global variables and objects
    b) The current file path
    c) Node.js version
    d) None of the above
    Answer: a) A list of all global variables and objects

Error Handling

  1. How does Node.js handle errors in asynchronous code?
    a) Using try-catch blocks
    b) By emitting an error event
    c) By returning null
    d) None of the above
    Answer: b) By emitting an error event
  2. What is the default behavior when an error event is not handled in Node.js?
    a) Logs the error and continues
    b) Crashes the application
    c) Ignores the error
    d) Automatically retries
    Answer: b) Crashes the application
  3. Which event is emitted when an error occurs in Node.js?
    a) error
    b) exception
    c) fault
    d) warning
    Answer: a) error
  4. How do you handle promise rejections in Node.js?
    a) Using try-catch blocks
    b) Using .catch()
    c) Using process.catch()
    d) None of the above
    Answer: b) Using .catch()
  5. Which global event handles uncaught exceptions in Node.js?
    a) process.on('error')
    b) process.on('uncaughtException')
    c) event.on('uncaught')
    d) None of the above
    Answer: b) process.on('uncaughtException')

Would you like me to provide the remaining 45 MCQs?

You said:

yes

ChatGPT said:

ChatGPT

Here are the remaining 45 MCQs to complete the set of 100:


Non-Blocking I/O

  1. What is Non-Blocking I/O in Node.js?
    a) Performing input/output operations sequentially
    b) Performing input/output operations without blocking the thread
    c) Performing synchronous file operations
    d) None of the above
    Answer: b) Performing input/output operations without blocking the thread
  2. Which of the following makes Node.js ideal for I/O-intensive applications?
    a) Synchronous operations
    b) Single-threaded architecture with Non-Blocking I/O
    c) Multithreaded architecture
    d) None of the above
    Answer: b) Single-threaded architecture with Non-Blocking I/O
  3. What does fs.readFile use in Node.js?
    a) Blocking I/O
    b) Non-Blocking I/O
    c) Both a and b
    d) None of the above
    Answer: b) Non-Blocking I/O
  4. Which callback parameter is usually used for handling errors in asynchronous functions?
    a) The first parameter
    b) The second parameter
    c) The last parameter
    d) None of the above
    Answer: a) The first parameter
  5. Which is true about Node.js's single-threaded architecture?
    a) It processes requests one at a time
    b) It uses an event loop for concurrent request handling
    c) It spawns multiple threads for each request
    d) It is not suitable for web applications
    Answer: b) It uses an event loop for concurrent request handling

Node.js File System Module

  1. Which module is used to work with files in Node.js?
    a) fs
    b) file
    c) system
    d) os
    Answer: a) fs
  2. How do you read a file synchronously in Node.js?
    a) fs.readFileSync()
    b) fs.readSync()
    c) fs.readFile()
    d) None of the above
    Answer: a) fs.readFileSync()
  3. Which method is used to append data to a file?
    a) fs.append()
    b) fs.appendFile()
    c) fs.addFile()
    d) fs.write()
    Answer: b) fs.appendFile()
  4. What does the fs.writeFile() function do?
    a) Creates and writes data to a file
    b) Appends data to an existing file
    c) Reads data from a file
    d) Deletes a file
    Answer: a) Creates and writes data to a file
  5. What happens if the file specified in fs.unlink() does not exist?
    a) A new file is created
    b) An error is thrown
    c) It does nothing
    d) It returns false
    Answer: b) An error is thrown

Node.js HTTP Module

  1. What is the primary use of the HTTP module in Node.js?
    a) To handle HTTP requests and responses
    b) To parse URLs
    c) To manage file systems
    d) None of the above
    Answer: a) To handle HTTP requests and responses
  2. Which method is used to create a server in Node.js?
    a) http.createServer()
    b) server.create()
    c) http.server()
    d) create.httpServer()
    Answer: a) http.createServer()
  3. What is the default HTTP port?
    a) 3000
    b) 8080
    c) 80
    d) 443
    Answer: c) 80
  4. Which event is emitted when the server receives a request?
    a) request
    b) data
    c) incoming
    d) connect
    Answer: a) request
  5. Which method is used to send a response back to the client?
    a) send()
    b) end()
    c) write()
    d) Both b and c
    Answer: d) Both b and c

Asynchronous Programming

  1. What is a callback in Node.js?
    a) A blocking function
    b) A function passed as an argument to another function
    c) A function that stops execution
    d) None of the above
    Answer: b) A function passed as an argument to another function
  2. Which statement is true about Promises in Node.js?
    a) They are used for synchronous operations
    b) They represent the eventual completion of an asynchronous operation
    c) They block the event loop
    d) None of the above
    Answer: b) They represent the eventual completion of an asynchronous operation
  3. Which of the following methods is used to handle resolved promises?
    a) .catch()
    b) .then()
    c) .finally()
    d) None of the above
    Answer: b) .then()
  4. Which of the following is true about async/await in Node.js?
    a) It blocks the main thread
    b) It simplifies writing asynchronous code
    c) It is synchronous by nature
    d) None of the above
    Answer: b) It simplifies writing asynchronous code
  5. Which Node.js version introduced async/await?
    a) 6
    b) 7
    c) 8
    d) 10
    Answer: c) 8

Node.js NPM (Node Package Manager)

  1. What does npm stand for?
    a) Node Package Manager
    b) Node Program Manager
    c) Node Processing Manager
    d) None of the above
    Answer: a) Node Package Manager
  2. What command is used to initialize a new Node.js project?
    a) npm start
    b) npm init
    c) npm new
    d) npm project
    Answer: b) npm init
  3. Which file is created by running npm init?
    a) index.js
    b) config.json
    c) package.json
    d) node_modules.json
    Answer: c) package.json
  4. How do you install a specific package using npm?
    a) npm install <package_name>
    b) npm add <package_name>
    c) npm setup <package_name>
    d) npm download <package_name>
    Answer: a) npm install <package_name>
  5. What does the -g flag do in npm commands?
    a) Installs the package globally
    b) Uninstalls the package
    c) Updates the package
    d) None of the above
    Answer: a) Installs the package globally

Miscellaneous

  1. Which of the following is a built-in Node.js module?
    a) os
    b) path
    c) events
    d) All of the above
    Answer: d) All of the above
  2. What is the output of console.log('Node.js is awesome!')?
    a) Error
    b) Node.js is awesome!
    c) node.js IS AWESOME!
    d) None of the above
    Answer: b) Node.js is awesome!
  3. What is the command to check the Node.js version?
    a) node -v
    b) node --version
    c) Both a and b
    d) None of the above
    Answer: c) Both a and b
  4. Which command clears the REPL screen in Node.js?
    a) .clear
    b) cls
    c) console.clear()
    d) Ctrl + L
    Answer: d) Ctrl + L
  5. Which method schedules a function to run after a specified delay?
    a) setTimeout()
    b) setInterval()
    c) runTimeout()
    d) delay()
    Answer: a) setTimeout()

Would you like me to continue with the remaining 15?

You said:

yes

ChatGPT said:

ChatGPT

Here are the remaining 15 MCQs:


Miscellaneous (continued)

  1. Which method schedules a function to run repeatedly at a fixed interval?
    a) setTimeout()
    b) setInterval()
    c) repeatTimeout()
    d) delayInterval()
    Answer: b) setInterval()
  2. What does process.exit() do in Node.js?
    a) Exits the application immediately
    b) Restarts the application
    c) Pauses the application
    d) Terminates all processes
    Answer: a) Exits the application immediately
  3. Which module is used to parse and format URLs in Node.js?
    a) url
    b) path
    c) query
    d) http
    Answer: a) url
  4. Which object provides information about the current Node.js process?
    a) process
    b) os
    c) system
    d) thread
    Answer: a) process
  5. What does the __dirname variable represent?
    a) The directory name of the current module
    b) The name of the currently executing file
    c) The absolute path to the Node.js binary
    d) None of the above
    Answer: a) The directory name of the current module
  6. Which statement about require() is true?
    a) It is used to load built-in modules only
    b) It is used to include modules in a Node.js application
    c) It blocks the event loop during module loading
    d) None of the above
    Answer: b) It is used to include modules in a Node.js application
  7. Which of the following is a valid file extension for a Node.js module?
    a) .node
    b) .js
    c) .json
    d) All of the above
    Answer: d) All of the above
  8. What does the buffer module in Node.js provide?
    a) Methods for working with binary data
    b) Methods for working with strings only
    c) Methods for managing processes
    d) None of the above
    Answer: a) Methods for working with binary data
  9. Which command uninstalls a package using npm?
    a) npm remove <package_name>
    b) npm uninstall <package_name>
    c) npm delete <package_name>
    d) npm erase <package_name>
    Answer: b) npm uninstall <package_name>
  10. How can you update a globally installed package in npm?
    a) npm update <package_name> -g
    b) npm upgrade <package_name>
    c) npm refresh <package_name>
    d) npm install <package_name> -u
    Answer: a) npm update <package_name> -g

Debugging in Node.js

  1. Which command is used to start debugging a Node.js application?
    a) node debug <file_name>
    b) node --inspect <file_name>
    c) node inspect <file_name>
    d) Both b and c
    Answer: d) Both b and c
  2. Which tool is commonly used to debug Node.js applications?
    a) Chrome DevTools
    b) Firebug
    c) Wireshark
    d) None of the above
    Answer: a) Chrome DevTools
  3. What is the purpose of the console.log() method?
    a) To log errors to a file
    b) To print output to the console
    c) To send HTTP logs
    d) To terminate the program
    Answer: b) To print output to the console
  4. Which method pauses execution in Node.js debugging mode?
    a) debug()
    b) pause()
    c) debugger
    d) None of the above
    Answer: c) debugger
  5. How can you print stack traces for debugging in Node.js?
    a) console.error()
    b) console.stack()
    c) console.trace()
    d) console.print()
    Answer: c) console.trace()

 

No comments:

Post a Comment

Search This Blog

Powered by Blogger.