Webd Qa
Webd Qa
1 Mark Answers:
3 Mark Answers:
• In dynamic websites, the server processes the request from the client, accesses the
database or other resources, and generates dynamic content (e.g., displaying personalized
information) before sending it back to the client.
5 Mark Answers:
Client-server computing architecture consists of two primary entities: the client and the server.
• Client: This is the device or software application that requests services or resources. It
could be a web browser, mobile app, or desktop application.
• Server: This is the machine or application that provides services or resources to the
client, like hosting websites, handling data, or processing requests.
Diagram:
Client Server
(Browser) (Web Server)
| |
Request (HTTP) Response (HTML, CSS, JS)
| |
Web Browser Web Server
In this architecture, clients send requests to the server, and the server processes and returns the
appropriate responses.
• Static Websites: These websites have fixed content. The web pages are simple HTML
files served to the client without modification.
o Advantages:
▪ Faster loading times.
▪ Simpler to develop.
▪ Less resource-intensive.
o Disadvantages:
▪ Limited interactivity.
▪ Difficult to update; requires manual changes to each page.
• Dynamic Websites: These websites generate content dynamically, often using a backend
programming language like PHP or Python, pulling data from a database.
o Advantages:
▪ Greater interactivity and personalization.
▪ Easier to update content; changes can be made in the backend without
altering the frontend.
o Disadvantages:
▪ Slower loading times due to server-side processing.
▪ More complex to develop and maintain.
• Client-side scripting (e.g., JavaScript): This script runs in the user's browser and is
responsible for tasks like form validation, DOM manipulation, and interactive UI
elements.
• Server-side scripting (e.g., PHP, Node.js): This script runs on the server, processing data
and generating dynamic content that is sent to the client's browser.
Interaction: When a user interacts with a dynamic webpage, client-side scripts capture user
actions (like button clicks), and may send data to the server (e.g., via AJAX). The server then
processes the data, queries a database if necessary, and sends back a response, which is then
handled by the client-side script to update the page without needing a full reload.
4. Describe the components of the World Wide Web and their functions.
• Web Browser: Displays web content (e.g., Chrome, Firefox). It retrieves and presents
data to the user.
• Web Server: Hosts websites and responds to client requests (e.g., Apache, Nginx).
• Web Page: The document displayed by the browser, usually written in HTML.
• URL (Uniform Resource Locator): The address that specifies the location of a resource
on the web.
• Hyperlinks: Allow navigation between different web pages.
• HTTP/HTTPS: Protocols that define how requests and responses are structured on the
web.
5. How does a web browser communicate with a web server? Explain the
process.
6. Write a detailed note on the differences between a web page, home page, and
website.
• Web Page: A single document on the web, usually consisting of HTML, CSS, and media
files. It is typically part of a larger website.
• Home Page: The main or introductory page of a website. It is often the first page users
see when visiting a site.
• Website: A collection of related web pages and resources, organized under a common
domain, providing a specific service or content.
JavaScript is a client-side scripting language used to create interactive and dynamic web pages. It
enables:
8. How do web servers handle requests from web clients? Explain with an
example.
When a web server receives a request from a client, it processes the request and responds
accordingly. For example:
If the request involves server-side processing (e.g., querying a database), the server might run a
script (like PHP), fetch data from the database, and send the resulting dynamic content back to
the client.
9. Explain the importance of scripting languages in creating interactive and
responsive websites.
Scripting languages (like JavaScript, PHP, Python) enable interactivity on websites by:
Unit 2:
1 Mark Answers:
3 Mark Answers:
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
o Unordered List (<ul>): A list where the items are bullet points.
<ul>
<li>First item</li>
<li>Second item</li>
</ul>
<meta charset="UTF-8">
<meta name="description" content="A website about cats">
o Importance:
▪ They improve SEO, control document behavior, and specify character
sets.
6. Use of the <table> Tag with Example:
o The <table> tag is used to create a table. It can contain <tr> for rows, <th> for
headers, and <td> for data cells.
o Example:
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
o Note: <frameset> and <frame> are deprecated in HTML5, and it's recommended
to use <iframe> instead.
9. Adding Images to an HTML Document:
o Images are added using the <img> tag with the src (source) attribute to specify
the image location and alt (alternative text) for accessibility.
o Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XHTML Page</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
5 Mark Questions
2. Explain the steps to create a table in HTML, including attributes for rows,
columns, and borders.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Example</title>
</head>
<body>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
3. Discuss the various input types supported in HTML forms with examples.
HTML forms support various <input> types for gathering different kinds of data:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Inputs</title>
</head>
<body>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name"><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>
<label for="age">Age:</label>
<input type="number" id="age" name="age"><br><br>
5. Explain the structure and purpose of meta tags with examples of common
attributes like name and content.
Meta tags provide metadata about an HTML document. They are placed in the <head> section of
the document and help browsers, search engines, and other services understand the content.
Common attributes:
• name: Specifies the name of the meta information (e.g., "description", "keywords").
• content: Provides the value for the specified meta name.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="This is a description of the webpage.">
<meta name="keywords" content="HTML, meta tags, web development">
<meta name="author" content="John Doe">
<meta http-equiv="refresh" content="30">
<title>Meta Tags Example</title>
</head>
<body>
<h1>Using Meta Tags</h1>
</body>
</html>
6. How can you create a web page using frames and framesets? Provide an
example.
Frames and framesets allow you to display multiple HTML documents within one web page.
• Use <frameset> instead of <body> to define the structure.
• Use <frame> to define each individual frame.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Frameset Example</title>
</head>
<body>
<frameset cols="25%, 75%">
<frame src="left.html" name="leftFrame">
<frame src="right.html" name="rightFrame">
</frameset>
</body>
</html>
Note: Frames are deprecated in HTML5 in favor of more modern layout techniques.
<h2>Image</h2>
<img src="example.jpg" alt="Example Image" width="300">
<h2>Links</h2>
<a href="https://www.example.com" target="_blank">Visit Example
Website</a>
<h2>List</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
9. Explain how character entities are used in HTML. Provide examples for at
least five entities.
Character entities are used in HTML to represent special characters that would otherwise be
interpreted as HTML code.
Examples:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Character Entities</title>
</head>
<body>
<p>Use < for less than and > for greater than.</p>
<p>Display & for the ampersand symbol.</p>
<p>"Quotes" in HTML.</p>
<p>© 2025 Example Corp.</p>
</body>
</html>
10. Compare HTML and XHTML in terms of syntax rules, features, and usage.
• HTML:
o Less strict syntax.
o Tags do not need to be closed in some cases (e.g., <li>, <br>).
o Attribute values are not always required to be quoted.
• XHTML:
o More strict syntax.
o Tags must be properly closed.
o All attributes must have quoted values.
o It must be served with the MIME type application/xhtml+xml.
Usage: HTML is more commonly used in modern web development, while XHTML is used in
applications requiring stricter standards or backward compatibility.
Unit 3:
1 Mark Answers:
2. Write the syntax for including an external CSS file in an HTML document.
<link rel="stylesheet" href="styles.css">
○ Inline CSS
○ External CSS
5. Which property is used to set the font size in CSS?
The font-size property is used to set the font size of text.
3 Mark Questions:
o Internal CSS: CSS is written inside the <style> tag within the <head> section of
the HTML document.
<style>
p {
color: blue;
}
</style>
<p>This is a blue paragraph.</p>
o External CSS: CSS is stored in a separate .css file and linked to the HTML file
using the <link> tag.
body {
background-image: url('background.jpg');
background-size: cover; /* Ensures the image covers the entire
background */
}
p {
font-family: 'Arial', sans-serif;
font-size: 16px;
color: #333333;
}
div {
margin: 20px; /* space outside the element */
padding: 10px; /* space inside the element */
}
table {
width: 100%;
border-collapse: collapse; /* Makes borders collapse into a
single line */
}
th, td {
border: 1px solid black;
padding: 10px;
text-align: center;
}
tr:nth-child(even) {
background-color: #f2f2f2; /* Zebra striping effect */
}
ul {
list-style-type: none; /* Removes default bullets */
}
ul li {
background: url('custom-bullet.png') no-repeat left center;
padding-left: 20px;
color: #444;
}
div {
width: 200px;
height: 100px;
background-color: #4CAF50;
border-radius: 15px; /* Rounded corners */
}
img {
float: left;
margin-right: 10px;
}
o Clear: The clear property is used to prevent elements from floating beside a
floated element. It forces the element to move below the floated elements.
div {
clear: both;
}
5 Mark Questions:
1. Write a CSS code to style a web page with background images, colors, and
borders for different sections.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Page</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-image: url('header-bg.jpg');
background-size: cover;
color: white;
padding: 20px;
text-align: center;
}
section {
background-color: #f4f4f4;
padding: 20px;
margin: 10px;
border: 2px solid #ccc;
}
footer {
background-color: #333;
color: white;
text-align: center;
padding: 10px;
margin-top: 20px;
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Web Page</h1>
</header>
<section>
<h2>Section 1</h2>
<p>This is the first section with a background color and border.</p>
</section>
<section>
<h2>Section 2</h2>
<p>This is the second section with a background color and border.</p>
</section>
<footer>
<p>© 2025 My Website</p>
</footer>
</body>
</html>
2. Explain the CSS box model in detail with a diagram and an example.
The CSS box model defines how elements are structured on the page and consists of the
following parts:
Diagram:
lua
Copy code
+-----------------------------+
| Margin |
| +-----------------------+ |
| | Border | |
| | +-----------------+ | |
| | | Padding | | |
| | | +-----------+ | | |
| | | | Content | | | |
| | | +-----------+ | | |
| | +-----------------+ | |
| +-----------------------+ |
+-----------------------------+
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Box Model Example</title>
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 30px;
}
</style>
</head>
<body>
<div class="box">
This is a box with padding, border, and margin.
</div>
</body>
</html>
3. How can CSS be used to style text and fonts? Write a CSS code to demonstrate
various text and font properties.
CSS can style text using properties like font-family, font-size, font-weight, text-align,
line-height, and color.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text and Font Styling</title>
<style>
h1 {
font-family: 'Arial', sans-serif;
font-size: 36px;
font-weight: bold;
text-align: center;
color: #2c3e50;
}
p {
font-family: 'Times New Roman', serif;
font-size: 18px;
line-height: 1.6;
color: #34495e;
}
</style>
</head>
<body>
<h1>Styled Heading</h1>
<p>This is a paragraph styled with different font properties such as
family, size, weight, and color.</p>
</body>
</html>
4. Discuss the difference between relative, absolute, fixed, and sticky positioning
in CSS with examples.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Positioning Example</title>
<style>
.relative {
position: relative;
top: 20px;
}
.absolute {
position: absolute;
top: 20px;
left: 50px;
}
.fixed {
position: fixed;
bottom: 10px;
right: 10px;
}
.sticky {
position: sticky;
top: 0;
background-color: #f1f1f1;
}
</style>
</head>
<body>
<div class="relative">This is a relatively positioned div.</div>
<div class="absolute">This is an absolutely positioned div.</div>
<div class="fixed">This is a fixed positioned div.</div>
<div class="sticky">This is a sticky positioned div.</div>
</body>
</html>
5. Write a CSS code to design a table with alternating row colors, borders, and
padding.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:nth-child(odd) {
background-color: #fff;
}
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
<tr>
<td>Row 3, Column 1</td>
<td>Row 3, Column 2</td>
</tr>
</table>
</body>
</html>
6. Explain how margins and paddings work in CSS with an example of nested
elements.
• Padding: The space between the content of an element and its border.
• Margin: The space outside an element's border, creating space between elements.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Margins and Padding</title>
<style>
.outer {
margin: 30px;
background-color: lightblue;
padding: 20px;
}
.inner {
margin: 15px;
background-color: lightgreen;
padding: 10px;
}
</style>
</head>
<body>
<div class="outer">
Outer Box
<div class="inner">Inner Box</div>
</div>
</body>
</html>
7. Write a CSS code to style an image gallery with hover effects using borders
and shadows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<style>
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.gallery img {
width: 100%;
border: 2px solid #ccc;
transition: transform 0.3s, box-shadow 0.3s;
}
.gallery img:hover {
transform: scale(1.1);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="gallery">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
</body>
</html>
8. How can CSS be used to style lists? Write a code to demonstrate styling
ordered and unordered lists differently.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Styled Lists</title>
<style>
ul {
list-style-type: square;
margin-left: 20px;
color: green;
}
ol {
list-style-type: decimal;
margin-left: 20px;
color: blue;
}
</style>
</head>
<body>
<h2>Unordered List</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h2>Ordered List</h2>
<ol>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ol>
</body>
</html>
9. Discuss how the z-index property works in CSS. Provide an example involving
overlapping elements.
The z-index property controls the stacking order of elements. Higher values are displayed on
top of lower values.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>z-index Example</title>
<style>
.box1 {
position: absolute;
top: 50px;
left: 50px;
width: 200px;
height: 200px;
background-color: red;
z-index: 1;
}
.box2 {
position: absolute;
top: 100px;
left: 100px;
width: 200px;
height: 200px;
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
header {
background-color: #333;
color: white;
padding: 15px;
text-align: center;
}
nav {
background-color: #444;
overflow: hidden;
}
nav a {
float: left;
display: block;
color: white;
padding: 14px 16px;
text-align: center;
text-decoration: none;
}
nav a:hover {
background-color: #ddd;
color: black;
}
main {
padding: 20px;
}
h1, h2 {
color: #333;
}
p {
font-size: 16px;
line-height: 1.6;
}
img {
width: 100%;
max-width: 600px;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<header>
<h1>My Website</h1>
</header>
<nav>
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#contact">Contact</a>
</nav>
<main>
<h2>Welcome to My Website</h2>
<p>This is a paragraph of text on my website. Below is an image.</p>
<img src="image.jpg" alt="Sample Image">
</main>
</body>
</html>
Unit 4:
1 Mark Answers:
1. What is JavaScript?
JavaScript is a programming language used to create interactive effects and dynamic
content on web pages.
2. Name two data types supported in JavaScript.
o String
o Number
3. What is the purpose of the alert() function in JavaScript?
The alert() function is used to display a popup message to the user.
4. Which operator is used for strict equality in JavaScript?
The === operator is used for strict equality, which checks both value and type.
5. Write the syntax for a for loop in JavaScript.
function myFunction() {
// code to be executed
}
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Output: Hello, Alice!
var x = 10;
x = 20; // Allowed
let y = 30;
y = 40; // Allowed
const z = 50;
z = 60; // Error: Assignment to constant variable.
5 == '5'; // true
o === (Strict Equality Operator): Compares both the value and the type, without
type conversion.
function checkEvenOdd(num) {
if (num % 2 === 0) {
console.log(num + " is even.");
} else {
console.log(num + " is odd.");
}
}
checkEvenOdd(4); // Output: 4 is even.
checkEvenOdd(7); // Output: 7 is odd.
var person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, " + this.name);
}
};
person.greet(); // Output: Hello, John
<form id="myForm">
<label for="email">Email:</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
<script>
document.getElementById("myForm").onsubmit = function(event) {
var email = document.getElementById("email").value;
var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,6}$/;
if (!regex.test(email)) {
alert("Please enter a valid email.");
event.preventDefault(); // Prevent form submission
}
};
</script>
5 Mark Questions:
// for loop
console.log("For loop output:");
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// while loop
console.log("While loop output:");
let count = 1;
while (count <= 3) {
console.log(count);
count++;
}
</script>
</body>
</html>
The Document Object Model (DOM) is a programming interface for HTML and XML
documents. It represents the structure of the document as a tree of objects where each object
corresponds to a part of the page (such as elements, attributes, and text).
Accessing elements:
Manipulating elements:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Example</title>
</head>
<body>
<h1 id="header">Original Heading</h1>
<button onclick="changeText()">Change Heading Text</button>
<script>
function changeText() {
document.getElementById("header").innerText = "Updated Heading";
document.getElementById("header").style.color = "green";
}
</script>
</body>
</html>
• Prompt: Displays a dialog box that asks the user for input.
• Confirm: Displays a dialog box with OK and Cancel buttons and returns true or false.
let num = 5;
console.log(`The factorial of ${num} is ${factorial(num)}`);
</script>
</body>
</html>
In JavaScript, events can be handled using event listeners, which are attached to HTML
elements. These listeners trigger a function when an event occurs, such as mouseover or click.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Handling Example</title>
</head>
<body>
<button id="myButton">Click Me</button>
<div id="hoverDiv" style="width: 200px; height: 100px; background-color:
lightblue; margin-top: 20px;">
Hover over me
</div>
<script>
// Click event
document.getElementById("myButton").addEventListener("click",
function() {
alert("Button clicked!");
});
// Mouseover event
document.getElementById("hoverDiv").addEventListener("mouseover",
function() {
this.style.backgroundColor = "lightgreen";
});
// Mouseout event
document.getElementById("hoverDiv").addEventListener("mouseout",
function() {
this.style.backgroundColor = "lightblue";
});
</script>
</body>
</html>
In JavaScript, objects are created using curly braces {}. An object can have properties (key-value
pairs) and methods (functions attached to the object).
Example:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
greet: function() {
console.log("Hello, " + this.firstName + " " + this.lastName);
}
};
// Accessing properties
console.log(person.firstName); // Output: John
// Calling a method
person.greet(); // Output: Hello, John Doe
• User-defined Functions: These are functions created by the user to perform specific
tasks.
function multiply(a, b) {
return a * b;
}
8. Write a JavaScript code that validates a form with fields for name, email, and
phone number.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
</head>
<body>
<form id="myForm" onsubmit="return validateForm()">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br><br>
<label for="phone">Phone:</label><br>
<input type="text" id="phone" name="phone"><br><br>
<script>
function validateForm() {
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
let phone = document.getElementById("phone").value;
return true;
}
</script>
</body>
</html>
The try-catch block is used to handle errors in JavaScript. Code inside the try block is
executed, and if an error occurs, control is passed to the catch block.
Example:
try {
let num = 10;
let result = num / 0; // This will not throw an error but return Infinity
console.log(result);
let undefinedVar = undefinedVar + 1; // This will throw an error
} catch (error) {
console.log("Error caught: " + error.message);
}
<script>
function changeContent() {
document.getElementById("demo").innerText = "This is the updated
text!";
}
</script>
</body>
</html>
Unit 5:
1 Mark Answers:
3 Mark Questions:
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<year>2023</year>
</book>
o Building Blocks:
▪ Element: <book>, <title>, <author>, <year>
▪ Text content: XML for Beginners, John Doe, 2023
▪ Attributes (optional): <book id="1"> (optional, not used in this
example)
▪ Prolog (optional): <?xml version="1.0"?> (optional, for version
declaration)
8. Using DTD to Define the Structure of an XML Document:
o DTD (Document Type Definition) defines the structure, allowed elements, and
attributes of an XML document.
o Example:
<!DOCTYPE book [
<!ELEMENT book (title, author, year)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
]>
<book>
<title>XML for Beginners</title>
<author>John Doe</author>
<year>2023</year>
</book>
book {
color: blue;
}
title {
font-weight: bold;
}
5 Mark Questions:
Features of XML:
• Human-readable and machine-readable: XML is text-based, making it both readable
by humans and easily processed by machines.
• Extensible: XML allows the definition of custom tags, making it adaptable to various
data types.
• Platform-independent: XML is platform-agnostic, meaning it can be used across
different systems and applications.
• Self-descriptive: XML elements describe their own data, providing context and meaning.
Benefits:
• Data interchange: XML is widely used for transferring data between different systems,
especially when the data source and destination are built with different technologies.
• Data storage: XML can be used to store data in a structured, hierarchical format.
• Standardized format: XML is standardized and widely adopted, ensuring compatibility
and interoperability across various applications and platforms.
3. Discuss the key differences between HTML and XML, focusing on syntax,
usage, and functionality.
4. What is DTD in XML? Write an example showing how DTD is used to define
the structure of an XML document.
DTD (Document Type Definition) defines the structure and rules for an XML document. It
specifies what elements are allowed, their order, and their attributes.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, publicationYear, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publicationYear (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
]>
<library>
<book>
<title>Introduction to Programming</title>
<author>John Doe</author>
<publicationYear>2021</publicationYear>
<isbn>978-1234567890</isbn>
</book>
<book>
<title>Web Development Essentials</title>
<author>Jane Smith</author>
<publicationYear>2020</publicationYear>
<isbn>978-0987654321</isbn>
</book>
</library>
5. Explain the concept of an XML Parser in detail. Compare DOM Parser and
SAX Parser with examples.
XML Parser: An XML parser reads and interprets XML data and provides an API to access the
document's structure and content.
catalog {
font-family: Arial, sans-serif;
}
product {
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
}
name {
font-weight: bold;
font-size: 1.2em;
}
price {
color: green;
}
category {
color: blue;
}
7. How does XML ensure data validation using DTD? Write a DTD to validate
an XML document for student data.
8. Discuss the advantages and challenges of using XML in data storage and
exchange.
Advantages of XML:
Challenges of XML:
<script>
var xmlString = `
<catalog>
<product>
<name>Smartphone</name>
<price>499.99</price>
</product>
<product>
<name>Laptop</name>
<price>899.99</price>
</product>
</catalog>`;
10. Compare and contrast the use of XML and JSON for data interchange.
Unit 6:
1 Mark Answers:
<?php
// PHP code here
?>
session_start();
7. What does $_POST in PHP represent?
$_POST is a superglobal array used to collect form data after submitting a form with the
POST method.
8. Write the syntax to connect to a MySQL database in PHP.
Example using mysqli:
3 Mark Questions:
o print: It is also a language construct used to output a single string. Unlike echo,
print returns a value (1), which makes it useful in expressions.
// process.php
echo $_GET['username']; // Outputs the value of the username
field
o $_POST: It is used to collect form data after submitting an HTML form with the
method POST. Data is sent in the HTTP request body, which is more secure for
sensitive data.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: " . $i . "<br>";
}
?>
javascript
Copy code
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
// Setting a cookie
setcookie("username", "Alice", time() + 3600, "/"); // Cookie
expires in 1 hour
include 'header.php';
o require: Similar to include, but if the file is not found, it results in a fatal error
and stops the script execution.
require 'header.php';
5 Mark Questions:
1. Write a PHP script to process a form and display the submitted data (e.g.,
name, email, and age).
<!DOCTYPE html>
<html>
<head>
<title>Form Submission</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
</body>
</html>
• If-Else Statement:
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
• Switch Statement:
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
default:
echo "Invalid day";
}
• Ternary Operator:
$age = 20;
echo ($age >= 18) ? "You are an adult." : "You are a minor.";
3. Write a PHP script that demonstrates the use of functions, including a user-
defined function and a built-in function.
<?php
// User-defined function
function greet($name) {
return "Hello, " . $name;
}
// Built-in function
$date = date("Y-m-d H:i:s");
echo "<br>Current Date and Time: " . $date;
?>
• Cookies:
o A cookie is stored on the user's browser and can be accessed across sessions.
o Example:
session_start();
$_SESSION['username'] = "JohnDoe";
echo "Hello, " . $_SESSION['username'];
Differences:
• Cookies are stored in the user's browser, while sessions are stored on the server.
• Cookies persist even after the browser is closed (until expiration), while sessions expire
when the browser is closed or the session is destroyed.
• Cookies have size limitations (typically 4KB), while sessions can store larger amounts of
data.
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert Data
if (isset($_POST['insert'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
$conn->query($sql);
}
// Update Data
if (isset($_POST['update'])) {
$id = $_POST['id'];
$newEmail = $_POST['newEmail'];
$sql = "UPDATE users SET email='$newEmail' WHERE id='$id'";
$conn->query($sql);
}
// Delete Data
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$sql = "DELETE FROM users WHERE id='$id'";
$conn->query($sql);
}
// Retrieve Data
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . " - Email: " .
$row['email'] . "<br>";
}
$conn->close();
?>
6. Explain the use of loops in PHP with examples of while, do-while, and foreach
loops.
• While Loop:
$i = 1;
while ($i <= 5) {
echo $i . "<br>";
$i++;
}
• Do-While Loop:
$i = 1;
do {
echo $i . "<br>";
$i++;
} while ($i <= 5);
7. How does PHP integrate with HTML? Write a sample PHP-HTML script for
a simple calculator.
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$sum = $num1 + $num2;
echo "Sum: " . $sum;
}
?>
</body>
</html>
8. Write a PHP program to handle file uploads and display the uploaded file
details.
<!DOCTYPE html>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has
been uploaded.";
echo "<br>File Details:<br>";
echo "File Name: " . $_FILES["fileToUpload"]["name"] . "<br>";
echo "File Size: " . $_FILES["fileToUpload"]["size"] . " bytes<br>";
echo "File Type: " . $_FILES["fileToUpload"]["type"] . "<br>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
</body>
</html>
9. Explain the process of database connectivity in PHP using MySQL, including
error handling.
<?php
// Database connection
$conn = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Perform a query
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "</table>";
$conn->close();
?>