KTU Web Programming QPs
KTU Web Programming QPs
1. ID Selector (#)
Example:
<html>
<head>
<style>
#header {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
</style>
</head>
<body>
</body>
</html>
2. Class Selector (.)
● The class selector is used to target multiple elements with the same class
attribute. This is useful when you want to apply the same style to a group of
elements.
● To apply styles with a class selector, you prefix the class name with a .
symbol in CSS.
Example:
<html>
<head>
<style>
.box {
padding: 20px;
margin: 10px;
</style>
</head>
<body>
</body>
</html>
Example:
You can also create arrays using the Array constructor. If you pass a single numeric
argument to the Array constructor, it creates an array of the specified length with
empty values. If you pass multiple arguments, they will become the elements of the
array.
Example:
3. Using Array.of()
The Array.of() method creates a new array instance with a variable number of
elements, regardless of the number or types of the arguments. It’s especially useful
for cases where you want to create an array with a single numeric element without
creating empty slots.
Example:
4. Using Array.from()
Example:
The spread operator can create arrays from existing arrays or other iterable objects
like strings or sets.
Example:
Q. Discuss the various CSS style sheet levels with suitable examples. How are
conflicts resolved when multiple style rules apply to a single web page element?
1. Inline CSS
● Inline CSS applies styles directly within an HTML element using the style
attribute. This is usually used for specific, one-time customizations.
● Inline styles have the highest specificity among the three methods
discussed here.
Example:
Example:
<head>
<style>
p{
color: blue;
font-size: 16px;
</style>
</head>
<body>
</body>
3. External CSS
● External CSS is defined in a separate .css file that is linked to the HTML
document via a <link> tag in the <head> section.
● It is the most scalable and preferred method for styling multiple pages with
consistent design.
● External CSS files are cached by the browser, improving performance on
repeat visits.
Example:
main.html
<head>
</head>
style.css
p{
color: green;
font-size: 14px;
When multiple CSS rules target the same HTML element, conflicts are resolved
based on the cascading and specificity rules:
<head>
<style>
.text {
color: blue;
</style>
</head>
<body>
</body>
#para {
color: green;
Resolution:
1. The <p> tag has an inline style (color: red;), giving it the highest
specificity.
2. The ID selector #para from the external stylesheet has high specificity but is
overridden by the inline style.
3. The internal class selector .text has lower specificity than the ID but would
override general element selectors.
So, the final color of the paragraph will be red, due to the inline style.
Q. List the order of precedence of style levels. Organize a sample web page for
providing ‘KTU BTech Honours Regulation 19’ for KTU and use embedded Style
sheet to apply minimum 5 styles for list, tables and pages.
2.Internal (Embedded) Styles: Defined within the <style> tag in the HTML
<head>.
3.External Styles: Linked from an external CSS file using the <link> tag in the
HTML <head>.
4.Browser Default Styles: Lowest priority and used if no other CSS is applied.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
background-color: #f3f4f7;
color: #333;
line-height: 1.6;
/* Heading style */
h1 {
color: #4CAF50;
text-align: center;
margin-top: 20px;
font-size: 2em;
/* List style */
ul.regulations-list {
list-style-type: square;
margin-left: 20px;
padding: 10px;
}
ul.regulations-list li {
margin-bottom: 8px;
color: #1f5f8b;
/* Table styles */
table {
width: 80%;
border-collapse: collapse;
th, td {
padding: 12px;
text-align: left;
th {
background-color: #4CAF50;
color: white;
td {
background-color: #fafafa;
</style>
</head>
<body>
<h2>Important Regulations</h2>
<ul class="regulations-list">
</ul>
<h2>Regulation Details</h2>
<table>
<tr>
<th>Regulation</th>
<th>Description</th>
</tr>
<tr>
<td>Eligibility</td>
</tr>
<tr>
<td>Credit Requirements</td>
</tr>
<tr>
<td>Grading</td>
</tr>
<tr>
<td>Attendance</td>
</tr>
<tr>
<td>Project Submission</td>
</tr>
</table>
</body>
</html>
This is the simplest and most common way to declare an array in JavaScript.
You can also declare an array using the Array constructor. Be cautious with this
approach if you're passing a single number, as it will create an array of that length
rather than an array with that number as an element.
3.Using Array.of()
4.Using Array.from()
join()
Example:
slice()
Example:
Q.Organize a sample web page for the event ‘Raagam2021’ at your campus and use
embedded Style sheets to apply a minimum 5 styles. State the Style Specification
format of embedded style sheets.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
background-color: #f0f8ff;
color: #333;
}
h1 {
color: #ff4500;
text-align: center;
font-size: 2.5em;
margin-top: 20px;
p.description {
width: 80%;
font-size: 1.1em;
line-height: 1.6;
text-align: justify;
.register-btn {
display: block;
width: 200px;
padding: 10px;
background-color: #ff4500;
color: white;
text-align: center;
font-size: 1.1em;
text-decoration: none;
border-radius: 5px;
.register-btn:hover {
background-color: #e03a00;
footer {
background-color: #333;
color: white;
text-align: center;
padding: 15px;
margin-top: 20px;
</style>
</head>
<body>
<h1>Welcome to Raagam2021</h1>
<p class="description">
</p>
<footer>
</footer>
</body>
</html>
a. to display the content of hyperlinks with yellow background color and in italics
To display hyperlinks with a yellow background color and in italics, you can target the
<a> (anchor) element.
a{
background-color: yellow;
font-style: italic;
}
b. Style for Unordered List Contents in Bold and Arial Font
To style the items within an unordered list (<ul>), you can target the <ul> and <li>
elements.
ul {
font-weight: bold;
body {
background-image: url("birds.jpg");
background-repeat: no-repeat;
background-size: cover; /* Optional: makes the image cover the entire screen */
1.Inline CSS
3.External CSS
1. Inline CSS
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
background-color: lightgray;
h1 {
color: green;
</style>
</head>
<body>
<h1>Welcome to the Page</h1>
</body>
</html>
3. External CSS
● Description: External CSS is written in a separate .css file and linked to the
HTML document using a <link> tag in the <head> section. This allows the
same stylesheet to be applied across multiple pages.
● Precedence: External styles are overridden by both inline and internal styles.
● Use Case: Best for large projects where many pages share the same styles.
It's the most scalable and efficient method.
Example:
<head>
</head>
body {
background-color: lightblue;
h1 {
color: darkblue;
}
4. Browser Default Styles
● Description: Every browser comes with its own default set of styles (also
known as "user agent styles"). These styles are applied if no other styles are
specified by the web developer.
● Precedence: These styles are overridden by any styles from inline, internal,
or external CSS.
● Use Case: Browser default styles provide basic formatting to web elements
like headings, paragraphs, links, and buttons.
Example: In the absence of any CSS rules, browsers apply their default styles to
elements like:
Q.write javaScript program to find factorial of a number,use prompt dialog box to get
the input from user.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Factorial Program</title>
<script>
function calculateFactorial() {
num = parseInt(num);
// Check if the input is a valid number
return;
let factorial = 1;
factorial *= i;
</script>
</head>
<body>
<h1>Factorial Calculator</h1>
</body>
</html>
Q.Write CSS and the corresponding HTML code for the following:
i. Set the background color for the hover and active link states to "yellow".
ii. Set the list style for ordered lists to "lower case alphabet".
iii. Set "boat.jpg" as the background image of the page and set 3% margin for the
page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Example</title>
<style>
/* i. Set the background color for the hover and active link states to "yellow". */
a:hover, a:active {
background-color: yellow;
/* ii. Set the list style for ordered lists to "lower case alphabet". */
ol {
list-style-type: lower-alpha;
/* iii. Set "boat.jpg" as the background image of the page and set 3% margin for
the page. */
body {
background-image: url('boat.jpg');
background-size: cover;
margin: 3%;
}
html {
</style>
</head>
<body>
<h1>Sample Document</h1>
<a href="#">Hover or click this link to see the yellow background effect</a>
<h2>Ordered List</h2>
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
</body>
</html>
Q.Explain various types of control statements in JavaScript with example
1. Conditional Statements
a) if Statement
b) if...else Statement
Executes one block of code if the condition is true, and another if the condition is
false.
} else {
console.log("Grade B");
} else {
console.log("Grade C");
d) switch Statement
switch (day) {
case "Monday":
break;
case "Wednesday":
console.log("Midweek");
break;
case "Friday":
console.log("Weekend is near!");
break;
default:
2. Looping Statements
a) for Loop
Executes a block of code a specific number of times.
b) while Loop
let i = 0;
while (i < 5) {
i++;
c) do...while Loop
Executes a block of code once and then repeats as long as the condition is true.
let i = 0;
do {
i++;
3. Jump Statements
Jump statements alter the flow of control by skipping or terminating certain blocks of
code.
a) break Statement
Terminates the loop or switch statement and transfers control to the statement
following the loop.
if (i === 3) {
break; // Exits the loop when i equals 3
console.log(i);
b) continue Statement
Skips the current iteration of the loop and proceeds with the next one.
if (i === 2) {
console.log(i);
c) return Statement
function square(num) {
console.log(square(4)); // Outputs 16
a) try...catch Statement
Executes code in the try block and handles any exceptions in the catch block.
try {
let result = 10 / 0;
console.log(result);
} catch (error) {
b) throw Statement
function checkAge(age) {
return "Welcome!";
try {
console.log(checkAge(15));
} catch (error) {