WC QB Soln
WC QB Soln
Definition: The Document Object Model (DOM) is a programming interface for web
documents, primarily HTML and XML.
Structure Representation:
The DOM represents a document as a tree-like structure where each node
corresponds to a part of the document (elements, attributes, text).
Node Types:
Element Nodes: Represent HTML tags.
Attribute Nodes: Represent the attributes of HTML elements.
Text Nodes: Represent the text content within elements.
Manipulation:
The DOM allows developers to access and manipulate the document's structure,
content, and style programmatically.
Dynamic Interaction:
By using scripting languages like JavaScript, developers can dynamically update
content, respond to user interactions, and create a responsive user experience.
Browser Independence:
The DOM is standardized, ensuring consistent behavior across different web browsers.
Use Cases:
Changing the content of a webpage without reloading.
Validating form input on the client side.
Creating dynamic and interactive web applications.
Importance:
The DOM is fundamental in modern web development, enabling the creation of
dynamic, interactive, and user-friendly websites.
1. Client-Server Architecture
The API should separate the user interface (client) from data storage (server). This
separation allows for the independent evolution of the client and server.
2. Statelessness
Each request from the client must contain all the necessary information for the server to
process it. The server does not store any session data, making each request independent.
3. Cacheability
Responses should indicate whether they are cacheable, allowing clients to reuse them for
future requests, which improves efficiency and reduces server load.
4. Uniform Interface
The API should have a consistent interface, making it easier to use. This is achieved by:
Resource Identification: Resources are identified by URIs (Uniform Resource
Identifiers).
Resource Manipulation: Resources are manipulated using standard HTTP methods
(GET, POST, PUT, DELETE).
Self-descriptive Messages: Each request and response contains enough information
to be understood in isolation.
HATEOAS: The client interacts with the API through hyperlinks provided by the server,
guiding the client on how to use the API.
5. Layered System
The API architecture should be composed of multiple layers, such as load balancers,
proxies, and gateways, allowing for scalability and flexibility.
The server can send executable code (e.g., JavaScript) to the client, which can be run on
the client-side. This is an optional feature and not required for an API to be RESTful.
4. Differentiate between JSON and XML. Discuss their use cases and
advantages in web development.
JSON XML
Parsing You can parse JSON with a You need to parse XML with
standard JavaScript an XML parser.
function.
Schema documentation JSON is simple and more XML is complex and less
flexible. flexible.
Ease of use JSON has smaller file sizes XML tag structure is more
and faster data complex to write and read
transmission. and results in bulky files.
Security JSON is safer than XML. You should turn off DTD
when working with XML to
mitigate potential security
risks
Use Cases of JSON in Web Development:
1. APIs and Web Services: Used in RESTful APIs for data exchange between clients and
servers.
2. Configuration Files: Ideal for storing application settings due to its simplicity.
3. NoSQL Databases: Used in databases like MongoDB for flexible data storage.
4. Client-Side Data Handling: Commonly used in JavaScript for dynamic content updates.
1. Legacy System Data Interchange: Used in B2B transactions and enterprise applications.
2. Document Storage: Suitable for storing structured documents like configuration files.
3. SOAP Web Services: Primary format for SOAP messages in enterprise environments.
4. RSS Feeds: Used for distributing content updates like news and blogs.
Advantages of JSON:
Lightweight and Faster: Less verbose, leading to faster transmission and parsing.
Easy to Use and Read: Simple syntax, especially in JavaScript environments.
Performance: More efficient for web and mobile apps.
Advantages of XML:
1. Presentation Layer
responsible for interacting with the user
handles tasks like displaying information, gathering user input, & formatting data for
presentation
consists UI components like html, css and javascript
2. Application Layer
responsible for processing user requests an applying business logic
tasks like validating user input, performing calculations & interfacing with the data tier
often implemented using Java, Python or PHP
3. Data Tier
responsible for storing & managing application data
consists DBMS like MySQL, Oracle or SQL Server
6. What is HTTP? Explain its working along with request and response
example.
HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the web. It is
a protocol used to transfer hypertext (HTML) documents, images, videos, and other resources
between a web server and a client (such as a web browser). HTTP defines how messages are
formatted and transmitted, and how web servers and browsers should respond to various
commands.
1. Client Request:
The process begins when a client (e.g., a web browser) sends an HTTP request to a
server.
The request specifies the resource the client wants to access (like a web page or an
image).
2. Server Processing:
The server receives the request, processes it, and determines the appropriate
response.
The server may access databases, run scripts, or retrieve files to generate the
response.
3. Server Response:
The server sends an HTTP response back to the client, which includes the requested
resource or an error message.
The response also contains status information about the request (e.g., whether it was
successful or not).
4. Client Receives and Processes:
The client (browser) receives the response and renders the content (e.g., displaying a
webpage) for the user.
When a user types a URL into their browser, the browser sends an HTTP request to the server
hosting that URL.
Example Request:
User-Agent: Mozilla/5.0
Accept: text/html
The server processes the request and sends back an HTTP response.
Example Response:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 3056
<html>
<head><title>Example Page</title></head>
<body><h1>Welcome to Example.com</h1></body>
</html>
200 OK: A status code indicating that the request was successful.
Content-Type: The type of content being sent (e.g., text/html).
Content-Length: The size of the response body in bytes.
Response Body: The actual HTML content of the page.
10. Differentiate ES5 and ES6. Give an example of the Anonymous and
Arrow function in ES6
ES5 ES6
It supports primitive data types that are In ES6, there are some additions to
string, number, boolean, null, and JavaScript data types. It introduced a new
undefined. primitive data type ‘symbol’ for supporting
unique values.
There is only one way to define the There are two new ways to define variables
variables by using the var keyword. that are let and const.
In ES5, both function and return keywords An arrow function is a new feature
are used to define a function. introduced in ES6 by which we don’t
require the function keyword to define the
function.
An anonymous function is a function that doesn't have a name. It's often used as a callback
function or passed as an argument to other functions.
Example:
setTimeout(function() {
}, 1000);
Arrow functions provide a more concise syntax for writing functions. They also lexically bind
the this value, making them particularly useful in certain scenarios like callbacks.
Example:
// Arrow function
function changeBackgroundColor() {
document.body.style.backgroundColor = getRandomColor();
}
setInterval(() =>
{
window.location.reload();
}
, 2000);
changeBackgroundColor();
</script>
</body>
</html>
<script>
const redButton = document.getElementById('redButton');
const greenButton = document.getElementById('greenButton');
redButton.addEventListener
('click', () =>
{
document.body.style.backgroundColor = 'red';
}
);
greenButton.addEventListener
('click', () =>
{
document.body.style.backgroundColor = 'green';
}
);
</script>
</body>
</html>
<h2>Registration Form</h2>
<form id="registrationForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<button type="submit">Register</button>
</form>
<script>
document.getElementById('registrationForm').addEventListener
('submit', function(event)
{
event.preventDefault();
document.getElementById('errorMessages').innerHTML =
errorMessages;
} else
document.getElementById('errorMessages').innerHTML = 'Form
submitted successfully!';
}
}
);
</script>
</body>
</html>
<div id="clock"></div>
<script>
function updateClock()
{
const now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
const timeString = hours + ':' + minutes + ':' + seconds;
document.getElementById('clock').textContent = timeString;
}
setInterval(updateClock, 1000);
updateClock();
</script>
</body>
</html>
<script>
function setCookie(name, value, days)
{
let expires = "";
if (days)
{
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
setCookie('username', 'Boy', 7);
</script>
</body>
</html>
const name="HelloWorld";
const ele = <h1>Welcome to {name}</h1>;
2. Virtual DOM:
DOM stands for Document Object Model. It is the most important part of the web
as it divides into modules and executes the code. Usually, JavaScript Frameworks
updates the whole DOM at once, which makes the web application slow. But react
uses virtual DOM which is an exact copy of real DOM. Whenever there is a
modification in the web application, the whole virtual DOM is updated first and
finds the difference between real DOM and Virtual DOM.
In the above-shown figure, when the whole virtual DOM has updated there is a
change in the child components. So, now DOM finds the difference and updates
only the changed part.
4. Performance:
As we discussed earlier, react uses virtual DOM and updates only the modified
parts. So , this makes the DOM to run faster. DOM executes in memory so we can
create separate components which makes the DOM run faster.
5. Extension:
React has many extensions that we can use to create full-fledged UI applications. It
supports mobile app development and provides server-side rendering. React is
extended with Flux, Redux, React Native, etc. which helps us to create good-looking
UI.
6. Conditional Statements:
JSX allows us to write conditional statements. The data in the browser is displayed
according to the conditions provided inside the JSX.
Syntax:
7. Components:
React.js divides the web page into multiple components as it is component-based.
Each component is a part of the UI design which has its own logic and design as
shown in the below image. So the component logic which is written in JavaScript
makes it easy and run faster and can be reusable.
8. Simplicity:
React.js is a component-based which makes the code reusable and React.js uses
JSX which is a combination of HTML and JavaScript. This makes code easy to
understand and easy to debug and has less code.
The React component lifecycle is a sequence of methods that are called at different stages of
a component's existence. These stages are broadly divided into three phases:
1. Initialization Phase
2. Mounting Phase
3. Updating Phase
4. Unmounting Phase
Here’s a breakdown of these phases and their associated lifecycle methods, including a
diagram for better understanding:
Initialization Phase
This phase is where the component is being set up before it is mounted. It includes:
Mounting Phase
This phase is when the component is being added to the DOM. It includes:
componentDidMount: Called once the component is mounted and is ideal for making API
calls or initializing data.
Updating Phase
This phase occurs when the component is being re-rendered due to changes in state or
props. It includes:
static getDerivedStateFromProps(props, state): Also called during this phase if the
component receives new props.
shouldComponentUpdate(nextProps, nextState): Determines if the component should
re-render based on changes to props or state.
render: The method where the component’s UI is described.
getSnapshotBeforeUpdate(prevProps, prevState): Called right before the changes are
applied to the DOM. It allows capturing some information (snapshot) from the DOM
before the update.
componentDidUpdate(prevProps, prevState, snapshot): Called after the component has
updated, which can be used for operations like network requests based on prop or state
changes.
Unmounting Phase
This phase is when the component is being removed from the DOM. It includes:
If there are errors during rendering, lifecycle methods, or in the constructor, you can use: