Node.js Exam
Node.js Exam
js Exam Answers
This Questions are related to Subject - Node JS , Make sure
to answer each question in either 5-6 bullet points or table,
keep it **very simple** to understand but at the same time
these answers will be written in exams. you can also explain
in analogy if the answer is difficult to learn but make sure
the analogy is not a part of the real answer it's just an extra
point
Got it! Drop your Node.js questions, and I'll give you clear, exam-friendly answers with
optional analogies for tough topics.
npm init
Syntax:
sh
Explanation:
npm init → Starts the package initialization process.
Analogy: Think of npm init like setting up a new profile on a website—you fill in some
details (or use defaults) before using it.
1/13
Command to Add express as a Dependency:
sh
sh
npm i express
javascript
// Create a connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'testdb'
});
// Connect to MySQL
connection.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL');
2/13
if (err) throw err;
console.log('Record Updated:', result.affectedRows);
});
connection.end();
});
Explanation:
Connects to MySQL database.
javascript
const fs = require('fs');
// Writing to a file
fs.writeFile('output.txt', content, (err) => {
if (err) throw err;
console.log('File written successfully!');
});
Explanation:
Imports the fs (File System) module.
3/13
Analogy: This is like writing a note in a diary— fs.writeFile creates the note, and if there's
an issue (like a missing pen), it throws an error.
2. Event Loop → Node.js listens for requests and processes them asynchronously.
3. Single Thread Execution → A single thread handles requests, delegating heavy tasks to
worker threads when needed.
4. Non-blocking I/O → Operations like file reading and database queries do not block
execution; they use callbacks/promises.
5. Response Sent → Once processing is complete, Node.js sends the response back to the
client.
Diagram Representation:
arduino
┌───────────────┐
│ Client 1 │
├───────────────┤
│ Client 2 │ → Request → ┌───────────────┐
├───────────────┤ │ Event Loop │
│ Client 3 │ → Request → │ (Handles I/O) │ → Processes request →
└───────────────┘ └───────────────┘ Sends response
4/13
Analogy:
Think of Node.js as a waiter in a restaurant. Instead of waiting for one dish to cook before
taking the next order, the waiter takes multiple orders, gives them to the kitchen (worker
threads), and serves food as soon as it's ready.
1. REPL
Marks: 2 each
Sections: Q1
Marks: 2 each
Sections: Q1
Example:
sh
$ node
> console.log("Hello");
Hello
5/13
2. Core Modules of Node.js
Core modules are built-in modules in Node.js.
Advantages of Node.js
1. Asynchronous & Non-blocking → Handles multiple requests simultaneously, improving
performance.
2. Fast Execution → Uses Google V8 Engine, making JavaScript code execution very fast.
3. Single Programming Language → Uses JavaScript for both frontend and backend,
simplifying development.
5. Large Community Support → Many open-source libraries and active developer support.
Analogy: Node.js is like a fast food drive-thru—it takes multiple orders at once and serves
them efficiently without making customers wait in line. 🚀
Write a program using addListener() method of
EventEmitter.
javascript
6/13
// Create an EventEmitter object
const myEmitter = new EventEmitter();
Explanation:
Imports the events module.
Note: addListener() works the same as on() , but on() is preferred in modern code.
Module Exports/Purpose
Purpose:
Example:
javascript
javascript
7/13
const math = require('./math');
console.log(math.add(5, 3)); // Output: 8
Analogy:
Think of module.exports like a menu in a restaurant—it defines what dishes (functions) are
available for customers (other files) to order.
Resource Usage High (creates new threads) Low (uses event loop)
Scalability Limited, needs more hardware Highly scalable without extra hardware
Analogy:
Traditional Server → Like a restaurant with one waiter per customer, leading to delays
if too many customers arrive.
Node.js Server → Like a fast food counter where one worker takes multiple orders
and serves them efficiently. 🚀
Explain Event-Driven Programming.
Definition:
8/13
Node.js uses an Event Loop to handle multiple events asynchronously.
Key Concepts:
2. Event Emitters → Objects that trigger and listen to events ( EventEmitter in Node.js).
Example:
javascript
myEmitter.on('greet', () => {
console.log('Hello! Event triggered.');
});
myEmitter.emit('greet');
Analogy:
Think of event-driven programming like a doorbell system—the bell rings (event), and
someone answers (handler), without constantly checking the door. 🚪🔔
Explain Anonymous function with an example.
Definition:
9/13
javascript
javascript
setTimeout(function() {
console.log("This runs after 2 seconds.");
}, 2000);
Analogy:
An anonymous function is like a one-time-use delivery guy—he delivers the package
(executes the function) and is never seen again! 🚀📦
1. File System Module Usage
Marks: 2 each
Sections: Q1
Marks: 2 each
Sections: Q1/Q5
10/13
Marks: 2/4 each
Sections: Q1/Q2
Marks: 4 each
Sections: Q4
Common Tasks:
javascript
11/13
2. Buffer.from(string) → Creates a buffer from a string.
javascript
javascript
2. Create a Server:
javascript
3. Listen on a Port:
javascript
2. Run Installer → Open the downloaded .msi file and follow the installation steps.
12/13
3. Choose Components → Ensure Node.js and npm (Node Package Manager) are
selected.
4. Set Environment Variables → The installer sets up the required system paths.
5. Verify Installation:
node -v
npm -v
13/13