1.Define Streams.
Briefly explain Different types of streams and their related
events with code snippets in Node.Js
A stream in Node.js is an abstract interface for working with streaming data.
Streams allow you to process data piece-by-piece (as chunks), rather than
waiting for the entire data to be available. This makes them ideal for handling
large files, network requests, and real-time data efficiently.
Types of Streams in Node.js
Stream Type Description Example
Readable Stream Used for reading data fs.createReadStream()
Writable Stream Used for writing data fs.createWriteStream()
Duplex Stream Both readable and writable net.Socket
Transform Stream Modify/transform data as read/write zlib.createDeflate()
• Readable Streams: Provide an interface for reading data.
• Writable Streams: Provide an interface for writing data.
• Duplex Streams: Combination; capable of both reading and writing
data.
• Transform Streams: Duplex streams that can modify or transform data
during writing or reading.
Stream Events in Node.js
Streams are instances of EventEmitter—they emit events you can handle in
your code. Here are key events:
• data: Emitted when data is available to read (Readable).
• end: Emitted when no more data to read (Readable).****error: Emitted
on an error (all streams).****finish: Emitted when all data has been
flushed to underlying system (Writable).
• close: Emitted when the stream closes (all streams).
const fs = require('fs');
const readStream = fs.createReadStream('input.txt');
const writeStream = fs.createWriteStream('output.txt');
readStream.on('data', (chunk) => {
console.log(Received ${chunk.length} bytes of data.);
writeStream.write(chunk);
});
readStream.on('end', () => {
console.log('Finished reading.');
writeStream.end();
});
writeStream.on('finish', () => {
console.log('Finished writing.');
});
2. Explain REPL in NodeJS
REPL in Node.js
The REPL (Read–Eval–Print–Loop) is an interactive programming environment
provided by Node.js that lets you enter JavaScript code and instantly see its
results. It's available by default with every Node.js installation and is
accessed by simply typing node in your command-line
interface.geeksforgeeks+2
How REPL Works
• Read: The REPL reads the user's input as a JavaScript expression.
• Eval: It evaluates the inputted code, executing it.
• Print: The result of the evaluation is printed to the console.
• Loop: This cycle repeats, allowing new inputs until the user decides to
exit (e.g., by pressing Ctrl+C twice or typing .exit).
Key Features
• Immediate Execution: Test code snippets and get results
instantly.tutorialspoint+1
• Multi-line Support: Write multi-line code blocks, such as functions or
loops.
• Special Variable (_): Stores the result of the previous expression for
quick reuse.nodejs
• Built-in Commands: Helpful commands
like .help, .exit, .break, .clear, .save, and .load.
• Experimentation: Useful for debugging, learning, and prototyping
JavaScript or Node.js code without the need to create files.
4. Write Shortnote on Props in react.js
Props (short for properties) in React.js are used to pass data from one
component to another, typically from a parent component to its child
components. They enable components to be dynamic and reusable by
allowing different inputs to be sent and rendered.
• Props are passed to components similarly to HTML attributes.***Inside
a component, props are accessed as an object, allowing retrieval of
individual values.
• Props are read-only, meaning a component cannot modify its own
props.****Props can hold various data types such as strings, numbers,
objects, arrays, or even functions.
• They enable communication between components and make UI
components customizable.
Example:
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;****}***<Greeting name="Alice" />
5. Explain the concepts of state in ReactS and demonstrate state management
with syntax and example
State in React: Concept & Example
State in React is a built-in object used to store and manage data that can
change over time and influence how a component behaves and renders. State
enables React components to be dynamic and interactive, as any change to
the state triggers a UI re-render to display the latest data. State is typically
used for variables like user input, toggles, fetched data, counters, etc.
How State Works
• State is local to each component by default.
• Changing state with its update function causes the component to re-
render.
• State should not be updated directly, but through setState() (class
components) or the updater function, e.g., setCount (function
components).
State Management in React
1. State in Functional Components (using useState)
Syntax: *** js
const [state, setState] = useState(initialState);
Where:
• state: Holds the current value.
• setState: Used to update state.
• initialState: Initial/default value.
Example: Counter Component
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0); // initial count is 0
const increment = () => setCount(count + 1); // Update state
return (***<div>
<h2>Counter: {count}</h2>
<button onClick={increment}>Increase</button>
</div>***);***}
export default Counter;
6. List and explain important features of MongoDB.
Important Features of MongoDB
1. Document-Oriented Database
• MongoDB stores data in flexible, JSON-like documents (BSON
format) instead of tables with rows and columns.***Documents
can have varying structures, making it schema-less and highly
adaptable for diverse data types.
2. Schema-less Database
• Collections in MongoDB do not require a fixed schema.***This
allows documents within the same collection to have different
fields and structures, giving developers flexibility to evolve their
data model without downtime.
3. High Scalability
• Supports horizontal scaling through sharding, which partitions
data across multiple servers.***Enables handling large volumes
of data and high throughput by distributing load.
4. Replication and High Availability
• MongoDB uses replica sets to maintain multiple copies of data
for redundancy.***Provides automatic failover and data
recovery if the primary server fails, ensuring high availability.
5. Powerful Indexing
• Every field in a document can be indexed to improve query
performance.***Supports various types of indexes (single field,
compound, geospatial, text).
6. Aggregation Framework
• Provides advanced data processing and transformation
capabilities similar to SQL’s GROUP BY.***Supports aggregation
pipelines, map-reduce functions, and single-purpose aggregation
methods.