Web Programming Module 2 Important Topics PYQs
Web Programming Module 2 Important Topics PYQs
Web Programming Module 2 Important Topics PYQs
Topics-PYQs
For more notes visit
https://rtpnotes.vercel.app
Web-Programming-Module-2-Important-Topics-PYQs
Important Topics
1. In-line, embedded, and external style sheets (Types of CSS)
2. z-index property in CSS
Example:
3. CSS method to position different HTML elements
1. Static (Default)
2. Relative
3. Absolute
4. Fixed
5. Sticky
4. Arrays in Javascript
5. Document Object Model concept in JavaScript
Examples
6. Media Queries
Media Queries in CSS
How Media Queries Work
Basic Syntax
Example: Changing Styles for Small Screens
Common Media Query Conditions
Previous Year Questions
1. Compare Absolute Positioning and Relative positioning.
Example in Action
Absolute Positioning
Relative Positioning
2. What is a call back function in JavaScript? State how it is different from normal
functions
Example of a Callback Function:
How is a Callback Different from Normal Functions?
3. Name components of box model?
The 4 Components of the CSS Box Model
4. List any 3 methods associated with javascript String object with their input
parameter and return type ?
5. Explain id selector and class selector in CSS with suitable examples
1. ID Selector
Example of ID Selector:
2. Class Selector
Example of Class Selector:
HTML Example:
12. Write CSS and the corresponding HTML code for the following:
HTML Code:
13. Develop embedded JavaScript that displays the cube of the number with font -
weight 400 and font-color green, entered through a prompt box?
14. Using Javascript function validate an HTML form containing textboxes for entering
username, password and a submit button. Validation should check the following
criteria, if it’s not satisfied an alert must be given.
15. Write JavaScript program to find factorial of a number, use prompt dialog box to
get the input from user.
16. How can CSS be used to display a XML document? Illustrate with an example.
Example of Using CSS to Style an XML Document:
XML Document:
CSS File (styles.css):
Explanation:
Important Topics
1. In-line, embedded, and external style sheets (Types of CSS)
1. Inline Styles
Styles are applied directly to an HTML element using the style attribute.
Example:
<head>
<style>
p {
color: green;
font-size: 18px;
}
</style>
</head>
Example:
<head>
<link rel="stylesheet" href="styles.css">
</head>
1. Default Value:
The default z-index is auto (treated as 0 if not set explicitly).
Elements with the same stacking context will appear in the order they are in the
DOM.
2. Higher Values:
Elements with a higher z-index value appear in front of elements with lower
values.
3. Stacking Context:
The z-index only works if the element has a position of relative , absolute ,
fixed , or sticky (not static , which is the default).
Example:
In this example:
The blue box will appear in front of the red box because its z-index is 2 , which is
higher than 1 .
div {
position: static; /* This is the default, no movement */
}
2. Relative
div {
position: relative;
top: 10px; /* Moves the element 10px down */
left: 20px; /* Moves the element 20px to the right */
}
3. *Absolute
Removes the element from the normal flow (it no longer affects other elements).
It is positioned relative to its closest parent that has positioning (or the whole page if no
parent is positioned).
Example:
div {
position: absolute;
top: 50px;
left: 100px; /* Moves it to 50px from the top and 100px from the left
*/
}
4. Fixed
div {
position: fixed;
bottom: 0;
right: 0; /* Stays in the bottom-right corner of the screen */
}
5. Sticky
div {
position: sticky;
top: 0; /* Sticks to the top of the page when scrolling */
}
4. Arrays in Javascript
JavaScript arrays are a special type of object that allows you to store multiple values in a single
variable. They are versatile and can hold various data types, including numbers, strings,
objects, and even other arrays. Arrays are zero-indexed, meaning the first element has an index
of 0.
Modifying Arrays
Adding Elements
Popping Elements
Length of array
console.log(fruits.length); // Outputs: 3
5. Document Object Model concept in JavaScript
The Document Object Model (DOM) represents the structure of an HTML document as a tree
of objects. Each element, attribute, and piece of text in the HTML document is represented as a
node in the DOM tree, enabling developers to manipulate the document's content and structure
programmatically.
Accessing the DOM: JavaScript provides several methods to access and manipulate DOM
elements:
Examples
Changing content
Media queries are a way to make your website responsive, meaning it adjusts to different
screen sizes or devices like phones, tablets, and desktops.
Media queries apply CSS styles only if certain conditions are met, such as screen width
or device type.
They help create designs that look good on all devices.
Basic Syntax
@media (condition) {
/* CSS rules for this condition */
}
1. max-width: Applies styles if the screen width is less than or equal to the value.
Example:
@media (max-width: 768px) {
p {
color: blue;
}
}
2. min-width: Applies styles if the screen width is greater than or equal to the value.
Example:
Example in Action
Absolute Positioning
The blue box is placed 50px from the top and 50px from the left of its parent (the gray
box).
Relative Positioning
The green box is shifted 20px down and 20px right from its original position.
2. What is a call back function in JavaScript? State how it is
different from normal functions
A callback function is a function that is passed as an argument to another function. It gets
called back (executed) after the main function has completed its operation.
function greet(name) {
console.log(`Hello, ${name}!`);
}
function processUserInput(callback) {
const name = "Alice";
callback(name); // Calls the callback function
}
In this example:
The CSS Box Model explains how every HTML element is represented as a rectangular box. It
consists of the following components:
1. Content
The actual stuff inside the box (like text, images, or other elements).
The size is controlled using width and height .
Example:
div {
width: 200px;
height: 100px;
}
2. Padding
The space between the content and the border.
It creates space inside the box around the content.
Controlled using padding , padding-top , padding-right , etc.
Example:
div {
padding: 10px;
}
3. Border
Example:
div {
border: 2px solid black;
}
4. Margin
Example:
div {
margin: 20px;
}
2. toUpperCase()
Description: Converts all characters in the string to uppercase.
Input Parameter:
None.
Return Type:
A new string with all characters in uppercase.
Example:
3. substring()
Description: Extracts a part of the string between two specified indices.
Input Parameters:
start (required): The starting index.
Return Type:
A new string containing the specified part of the original string.
Example:
Example of ID Selector:
<!DOCTYPE html>
<html>
<head>
<title>ID Selector Example</title>
<style>
#header {
color: blue;
font-size: 24px;
}
</style>
</head>
<body>
<h1 id="header">This is the Header</h1>
</body>
</html>
Explanation: The #header selector styles the <h1> element with the id="header" and
changes its text color to blue and font size to 24px.
2. Class Selector
Definition: A Class selector targets elements that have a specific class attribute.
Syntax: The class selector is written with a dot (.) symbol followed by the class name.
Reusability: Unlike IDs, classes can be reused on multiple elements within a page.
Example of Class Selector:
<!DOCTYPE html>
<html>
<head>
<title>Class Selector Example</title>
<style>
.highlight {
background-color: yellow;
padding: 10px;
}
</style>
</head>
<body>
<p class="highlight">This paragraph is highlighted.</p>
<div class="highlight">This div is also highlighted.</div>
</body>
</html>
Explanation: The .highlight class applies yellow background and padding to both
the <p> and <div> elements, demonstrating that a class can be applied to multiple
elements.
The array literal is the most common and simple way to create an array. It is created by placing
elements inside square brackets [] , separated by commas.
Example:
Here, fruits is an array containing three string elements: "Apple" , "Banana" , and
"Cherry" .
The Array.of() method creates a new array instance with a variable number of elements,
passed as arguments.
Example:
This method is useful when you need to create an array from a set of values.
The Array.from() method creates a new array from an array-like object or an iterable
object (like strings, NodeLists, etc.).
Each method has its specific use case, and you can choose the one that best fits your needs
when creating arrays in JavaScript.
Explanation:
font-style: italic; : This makes the text of the hyperlinks appear in italics.
Example HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Styling Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
/* 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; /* Uses lowercase alphabet for ordered list
items */
}
1. Conditional Statements
Conditional statements allow you to execute different blocks of code based on certain
conditions.
if (x > 5) {
console.log("x is greater than 5");
}
if-else statement: Executes one block of code if the condition is true , otherwise
executes another block.
if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is 5 or less");
}
if (x > 10) {
console.log("x is greater than 10");
} else if (x > 5) {
console.log("x is between 6 and 10");
} else {
console.log("x is 5 or less");
}
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid day");
}
2. Looping Statements
let i = 0;
while (i < 5) {
console.log(i); // Prints 0, 1, 2, 3, 4
i++;
}
do...while loop: Executes a block of code once, and then repeats it while the condition
is true .
let i = 0;
do {
console.log(i); // Prints 0, 1, 2, 3, 4
i++;
} while (i < 5);
3. Jump Statements
Jump statements allow you to break out of loops or functions and control the flow in other ways.
continue statement: Skips the current iteration of a loop and continues with the next
iteration.
function add(a, b) {
return a + b; // Exits the function and returns the result
}
Exception handling is used to deal with errors and exceptional conditions in a graceful manner.
try...catch statement: Allows you to handle errors without stopping the execution of
the program.
try {
let result = riskyFunction();
} catch (error) {
console.log("An error occurred: " + error.message);
}
try {
let result = riskyFunction();
} catch (error) {
console.log("An error occurred: " + error.message);
} finally {
console.log("This always runs");
}
An event handler can be defined directly within an HTML tag using attributes like onclick ,
onmouseover , etc.
Example:
<button onclick="alert('Button clicked!')">Click Me</button>
When the button is clicked, the alert() function will be triggered and display a message.
You can register an event handler by assigning a function to the event property of an HTML
element (e.g., onclick , onmouseover , etc.).
Example:
Example:
When the button is clicked, the event handler attached via addEventListener() will be
executed.
Html Code
<!DOCTYPE html>
<html>
<head>
<title>Event Handler Example</title>
</head>
<body>
<script>
// Get references to the HTML elements
const button = document.getElementById('changeTextBtn');
const message = document.getElementById('message');
</body>
</html>
1. HTML Structure:
A paragraph ( <p> ) with the id message that initially displays the text "Welcome".
A button ( <button> ) with the id changeTextBtn that is titled "Click".
2. JavaScript:
The getElementById() method is used to get references to the paragraph and
button.
The changeText() function changes the content of the paragraph to "Hello from
JavaScript" in bold.
The addEventListener('click', changeText) is used to register the event
handler for the click event on the button. When the button is clicked, the
changeText() function is triggered, and the paragraph content is updated.
Class selectors in CSS are used to apply styles to elements with a specific class attribute. The
class selector is defined with a dot ( . ) followed by the class name.
For example, if you have an HTML element with a class blue-text , you can apply CSS rules
to all elements with that class using the following syntax:
.blue-text {
/* CSS rules */
}
Solution:
To achieve the two styles for <p> tags depending on the context, we will use class selectors.
Here's how we can define the CSS for both styles:
CSS Code:
HTML Example:
Now, in the HTML, you can apply the classes to <p> tags
<!DOCTYPE html>
<html>
<head>
<title>Class Selectors Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
</body>
</html>
12. Write CSS and the corresponding HTML code for the
following:
i. Set the background color for the hover and active link states to "green"
ii. Set the list style for unordered lists to "square".
iii. Set "Flower.png" as the background image of the page .
iv. Set dashed border for left and right and double border for top & bottom of a table with 2
rows.
/* i. Set background color for hover and active link states to green */
a:hover, a:active {
background-color: green;
}
/* iv. Set dashed border for left and right and double border for top &
bottom of a table */
table {
border-left: 2px dashed black;
border-right: 2px dashed black;
border-top: 4px double black;
border-bottom: 4px double black;
width: 100%; /* Optional, to make the table width 100% of the page */
}
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Cube of a Number</title>
<style>
/* CSS for styling the output */
.output {
font-weight: 400;
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<script>
// Prompt user to enter a number
var number = prompt("Enter a number to calculate its cube:");
</body>
</html>
<h2>Login Form</h2>
<form name="loginForm" onsubmit="return validateForm()">
<!-- Username Field -->
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<script>
// Function to validate the form
function validateForm() {
var username = document.forms["loginForm"]["username"].value;
var password = document.forms["loginForm"]["password"].value;
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<script>
// Function to calculate factorial
function calculateFactorial(num) {
let factorial = 1;
</body>
</html>
To style an XML document, we can create a CSS file and reference it within the XML document
using the <?xml-stylesheet?> processing instruction.
XML Document:
<catalog>
<book>
<title>Learn CSS</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book>
<title>Advanced JavaScript</title>
<author>Jane Smith</author>
<price>39.99</price>
</book>
</catalog>
/* styles.css */
catalog {
background-color: lightgray;
padding: 10px;
}
book {
margin-bottom: 15px;
padding: 5px;
border: 1px solid #ccc;
}
title {
font-size: 18px;
font-weight: bold;
color: darkblue;
}
author {
font-style: italic;
}
price {
color: green;
font-weight: bold;
}
Explanation: