Unit 3 Exercise Question-Answers
Unit 3 Exercise Question-Answers
Programming Fundamentals
MCQ’s Answer:
1 C 6 C
2 C 7 C
3 C 8 D
4 C 9 B
5 D 10 C
Purpose To inform, educate, or entertain users. To enable users to perform specific tasks or
services.
Technology Built with HTML, CSS, and basic Built with complex technologies like
JavaScript. server-side scripting.
Examples School website with information about Online learning platform for accessing
events, history, and contact details. lessons, submitting assignments, and
receiving grades.
1 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
• The href attribute is used inside an <a> tag, ‘a’ stands for "anchor." The <a> tag creates a hyperlink that we
can click on.
• The href value is the URL (Uniform Resource Locator) of the page that we want to link to.
• Example of Using href:
<a href="https://www.google.com">Goto the Google Website</a>
Q. 3 Enlist the optional parameters to open a webpage.
A URL (Uniform Resource Locator) is the address used to access a webpage, an image, or a file on the
internet. It is composed of several parts. Here’s a breakdown of the parts of a URL:
https://www.example.com:8080/folder/page.html?search=query&category=books#section1
• https:// Protocol (Optional)
• www.example.com: Domain Name
• :8080 Port Number (Optional)
• /folder/page.html: Path (Optional)
• ?search=query&category=books Query String (Optional)
• #section1 Fragment (Optional)
Each part plays a crucial role in guiding the browser to the correct resource and displaying it appropriately.
Q. 4 List out the frequent tags used in text of a webpage and what are they used for?
2 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
<em> When we need to emphasize a word but with italics.
3 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
Q. 1 What is the Document Object Model? Explain with the help of an example.
The Document Object Model (DOM) is a programming interface for web documents. It represents the
structure of a document (like an HTML or XML document) as a tree of objects, allowing programs and
4 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
scripts to dynamically access, modify, and update the content, structure, and style of the document.
Tree Structure: The DOM represents the document as a tree structure where each node is an object
representing a part of the document (like elements, attributes, or text).
Example of DOM:
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<h1 id="main-heading">Hello, World!</h1>
<p class="description">This is a paragraph about the DOM.</p>
<button onclick="changeText()">Click Me</button>
<script>
function changeText()
{
// Access the element with id 'main-heading'
var heading = document.getElementById("main-heading");
Explanation:
5 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
1. HTML Structure: The HTML document includes a heading, a paragraph, and a button.
2. DOM Representation:
• The DOM represents this document as a tree with a root node (<html>).
• The <html> node has two child nodes: <head> and <body>.
• The <body> node contains child nodes like <h1>, <p>, and <button>.
Here’s an HTML example that visually differentiates between different heading levels (<h1> to <h6>)
using CSS styling: <h1> is the biggest and <h6> is the smallest heading.
<!DOCTYPE html>
<html>
<head>
<title>Differentiate Headings</title>
<style>
/* Styling each heading type with different colors and sizes */
h1 {
color: red;
font-size: 32px;
}
h2 {
color: blue;
font-size: 28px;
}
h3 {
color: green;
font-size: 24px;
}
h4 {
color: orange;
font-size: 20px;
}
h5 {
6 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
color: purple;
font-size: 18px;
}
h6 {
color: brown;
font-size: 16px;
}
</style>
</head>
<body>
<!-- Different Heading Levels -->
<h1> Heading Level 1 (H1) </h1>
<h2> Heading Level 2 (H2) </h2>
<h3> Heading Level 3 (H3) </h3>
<h4> Heading Level 4 (H4) </h4>
<h5> Heading Level 5 (H5) </h5>
<h6> Heading Level 6 (H6) </h6>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('img_girl.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
</head>
<body>
</body>
7 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
</html>
To add a background image on an HTML element, use the <style> element, in the <head> section. If we want
the entire page to have a background image, we must specify the background image on the <body> element.
If the background image is smaller than the element, the image will repeat itself horizontally and vertically
until it reaches the element's end.
To avoid the background image from repeating itself, set the background-repeat property to no-repeat. If we
want the background image to cover the entire element, we can set the background-size property to cover.
Also, to make sure the entire element is always covered, set the background-attachment property to fixed:
This way, the background image will cover the entire element, with no stretching (the image will keep its
original proportions).
Q. 4 With the help of simple code, highlight different methods to incorporate CSS code in an
HTML webpage.
There are three methods via which we can incorporate CSS code in an HTML page.
1. Inline CSS
Any CSS attribute that we want to incorporate can be added by using an HTML tag in the body section.
<P style =”color : red; font-size : 40px; font-style : italic; text-align : center;”>
My teaching academy
</p>
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
8 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
}
</style>
</head>
<body>
</body>
</html>
3. External CSS
A file with the extension “.CSS” can be made to apply all the styles. Once, the HTML file is finalized, just
attach that CSS file in the head section of HTML by passing the link. External CSS is used with large
projects.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css"> // rel: Specifies the relationship between the current
document and the linked document
</head>
<body>
</body>
</html>
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Q. 5 Sketch steps and provide code to apply border and color to a table in a webpage.
We can apply borders and color to a table by using CSS. For this purpose, we initially need to state
the “style” pair tag just before starting the table. We need to set which style and where to apply it. For the
border, we’ll define it for the table, table header cells, and every other cell.
9 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
<style>
table, th, td
{
border: 1px, solid Black;
}
</style>
We can also designate a background color for the table by adding the following code.
<style>
th, td
{
background-color: aqua;
}
</style>
Q. 6 Discuss the functionality JavaScript can provide in a webpage with the help of a suitable
example code.
JavaScript is an exciting language primarily used in the development of web pages and scripts. It
doesn’t consume much memory that is why it is used at the client end in developing websites for making
pages dynamic. JavaScript code can be embedded in HTML with a paired tag of “script”. There is no
limitation of where to place the code inside an HTML file.
In website development, the easiest way to introduce dynamicity is to allow some events to occur and respond
accordingly. Alert is commonly used functionality that JavaScirpt provides to inform the user about his
action.
Code Example:
<!DOCTYPE html>
<html>
<head>
<script>
function msgsure( )
{
alert ("Are you Sure?")
}
</script>
</head>
<body>
</body>
</html>
10 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
Example Code:
<html>
<head>
<title> Scrolling Text Example </title>
</head>
<body>
<!-- Simple Scrolling Text -->
<marquee> Welcome to our website! This text is scrolling. </marquee>
<!-- Scrolling Text with Customizations -->
< marquee direction="right" scrollamount= "10" > This text is scrolling from right to left! </marquee>
Q. 8 Enlist steps to add a video clip in a website which starts playing as the web page loads.
11 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
Example Code:
<html>
<head>
<title> Auto-Play Video Example </title>
</head>
<body>
<video width="600" height="400" autoplay>
<source src="video.mp4" type="video/mp4">
</video>
</body>
</html>
12 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
Q. 9 Cite steps on compiling the result of your last examination in a tabular form and display it in
a webpage.
Example Code:
<html>
<head>
<title> Exam Results </title>
<style>
13 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
table {
width: 60%;
border-collapse: collapse;
margin: 20px auto;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2 style="text-align: center;"> Exam Results </h2>
<table>
<tr>
<th>Student Name</th>
<th>Subject</th>
<th>Marks</th>
<th>Grade</th>
</tr>
<tbody>
<tr>
14 Waseem Hassan
HOD (Dept. of Computer Science)
Unit 3 Programming Fundamentals
<td>Muhammad Ali</td>
<td>Math</td>
<td>85</td>
<td>A</td>
</tr>
<tr>
<td>Ibrahim Khalil</td>
<td>Science</td>
<td>78</td>
<td>B</td>
</tr>
<tr>
<td>Muhammad Tariq</td>
<td>English</td>
<td>92</td>
<td>A</td>
</tr>
<tr>
<td>Sharjeel Khan</td>
<td>History</td>
<td>67</td>
<td>C</td>
</tr>
</tbody>
</table>
</body>
</html>
15 Waseem Hassan
HOD (Dept. of Computer Science)