0% found this document useful (0 votes)
16 views18 pages

WT Question Bank 20 Marks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

WT Question Bank 20 Marks

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1. What are some common DOM events in JavaScript?

Provide examples of handling mouse events


(e.g., click, mouseover).

Common DOM Events:

DOM events are actions that occur within the browser, such as a mouse click, a keypress, or page
load. Here are some categories of commonly used events:

1. Mouse Events:

o click: Triggered when an element is clicked.

o dblclick: Fired when an element is double-clicked.

o mouseover: Fired when the mouse pointer hovers over an element.

o mouseout: Fired when the mouse pointer leaves an element.

2. Keyboard Events:

o keydown: Triggered when a key is pressed down.

o keyup: Triggered when a pressed key is released.

3. Form Events:

o submit: Triggered when a form is submitted.

o change: Fired when a form element’s value changes.

4. Window Events:

o load: Triggered when the entire webpage (including images and CSS) loads.

o resize: Triggered when the browser window is resized.

Example of Handling Mouse Events:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<style>

#hoverBox {

width: 100px;

height: 100px;

background-color: lightblue;

}
</style>

</head>

<body>

<button id="clickButton">Click Me</button>

<div id="hoverBox"></div>

<script>

// Handling 'click' event

document.getElementById("clickButton").addEventListener("click", () => {

alert("Button clicked!");

});

// Handling 'mouseover' event

document.getElementById("hoverBox").addEventListener("mouseover", () => {

document.getElementById("hoverBox").style.backgroundColor = "green";

});

// Handling 'mouseout' event

document.getElementById("hoverBox").addEventListener("mouseout", () => {

document.getElementById("hoverBox").style.backgroundColor = "lightblue";

});

</script>

</body>

</html>

Explanation:

 click Event: Alerts the user when the button is clicked.

 mouseover Event: Changes the background color of the box when the mouse hovers over it.

 mouseout Event: Resets the background color when the mouse leaves the box.

2. Write a JavaScript code snippet to demonstrate handling multiple events on a single element.
Handling multiple events on a single element means attaching more than one listener to a single
DOM element.

Example:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<title>Multiple Events</title>

<style>

#testButton {

padding: 10px;

font-size: 16px;

</style>

</head>

<body>

<button id="testButton">Hover or Click Me</button>

<script>

const button = document.getElementById("testButton");

// 'mouseover' event

button.addEventListener("mouseover", () => {

button.style.backgroundColor = "yellow";

});

// 'mouseout' event

button.addEventListener("mouseout", () => {

button.style.backgroundColor = "";

});
// 'click' event

button.addEventListener("click", () => {

alert("Button clicked!");

});

</script>

</body>

</html>

Explanation:

1. The mouseover event changes the background color of the button when the mouse hovers
over it.

2. The mouseout event restores the original background color when the mouse leaves.

3. The click event triggers an alert when the button is clicked.

3. Explain the role of media queries in CSS3 and how they contribute to responsive design.

Role of Media Queries:

Media queries in CSS3 allow developers to create styles that adapt to different device characteristics
such as screen size, orientation, or resolution. They are fundamental to responsive web design,
which ensures websites function and display properly on all devices, from desktops to smartphones.

Syntax:

css

Copy code

@media (condition) {

/* CSS rules */

How Media Queries Contribute to Responsive Design:

1. Flexibility: Adjusts layouts for different screen sizes.

2. Improved User Experience: Ensures usability and readability across devices.

3. Device-Specific Features: Supports features like dark mode or orientation changes.

Example: Responsive Navigation Bar:

html

Copy code
<!DOCTYPE html>

<html lang="en">

<head>

<style>

body {

font-family: Arial, sans-serif;

.navbar {

background-color: #333;

padding: 10px;

color: white;

.navbar h1 {

font-size: 24px;

/* For devices with a max width of 600px */

@media (max-width: 600px) {

.navbar h1 {

font-size: 18px;

text-align: center;

</style>

</head>

<body>

<div class="navbar">

<h1>Responsive Navbar</h1>

</div>

</body>

</html>
Explanation:

 On devices with a width greater than 600px, the font size of the header remains large.

 On smaller screens, the font size reduces, and the header aligns to the center, improving
usability.

5. Explain the use of alert() in JavaScript. Provide an example of when it might be useful.

Use of alert():

The alert() method in JavaScript is used to display a simple pop-up box with a message. It is
synchronous, meaning it halts the execution of code until the user closes the alert box.

Syntax:

javascript

Copy code

alert("Your message here");

Example of Use:

1. Form Validation: Alert users of missing or invalid input.

2. Debugging: Show variable values during development.

Example Code:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<title>Alert Example</title>

</head>

<body>

<form id="myForm">

<label for="name">Name:</label>

<input type="text" id="name" />

<button type="submit">Submit</button>

</form>

<script>
document.getElementById("myForm").addEventListener("submit", (event) => {

const name = document.getElementById("name").value;

if (!name) {

event.preventDefault();

alert("Please enter your name!");

});

</script>

</body>

</html>

Explanation:

 If the user tries to submit the form without entering their name, an alert box appears,
preventing submission and prompting them to correct the mistake.

6. What are CSS3 transitions and animations? Explain with an example.

CSS3 Transitions:

CSS3 transitions allow you to make property changes on an element over time. Instead of
immediately changing properties when an event occurs, CSS transitions allow the properties to
transition smoothly.

 Syntax:

css

Copy code

selector {

transition: property duration timing-function delay;

 Property: The CSS property to transition (e.g., background-color, height).

 Duration: The length of time the transition lasts (e.g., 0.5s).

 Timing Function: Describes how the intermediate values of the property change (e.g., ease,
linear).

 Delay: The delay before the transition starts.

Example of CSS3 Transition:

html
Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<style>

.box {

width: 100px;

height: 100px;

background-color: lightblue;

transition: background-color 0.5s ease, transform 0.5s;

.box:hover {

background-color: lightgreen;

transform: scale(1.2);

</style>

</head>

<body>

<div class="box"></div>

</body>

</html>

Explanation:

 When the user hovers over the .box element, the background color transitions from light
blue to light green over 0.5 seconds, and the element scales up by 1.2 times.

CSS3 Animations:

CSS3 animations provide more control compared to transitions. They allow elements to be animated
using keyframes, making it possible to define multiple stages of the animation, and they can run
continuously or in a loop.

 Syntax:

css

Copy code
@keyframes animation-name {

0% { property: value; }

100% { property: value; }

selector {

animation: animation-name duration timing-function iteration-count;

Example of CSS3 Animation:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<style>

@keyframes bounce {

0% { transform: translateY(0); }

50% { transform: translateY(-30px); }

100% { transform: translateY(0); }

.box {

width: 100px;

height: 100px;

background-color: lightblue;

animation: bounce 1s infinite;

</style>

</head>

<body>

<div class="box"></div>
</body>

</html>

Explanation:

 The .box element will bounce up and down infinitely with the bounce animation, which
moves the element vertically (up 30px and back down).

7. Explain what XML is and discuss the key differences between XML and HTML, focusing on their
purpose, structure, and usage.

What is XML?

XML (Extensible Markup Language) is a markup language used to store and transport data. It allows
developers to create their own tags to describe data, making it more flexible than HTML. XML is
primarily used for data interchange between systems and applications, while HTML is focused on
displaying data in a browser.

Key Differences Between XML and HTML:

Feature XML HTML

Used to structure and display data on web


Purpose Used to store and transport data.
pages.

Structure User-defined tags (e.g., <book>). Predefined tags (e.g., <div>, <p>).

Case Tags are case-sensitive (e.g., <Book> vs Tags are case-insensitive (e.g., <div> is the
Sensitivity <book>). same as <DIV>).

Must be well-formed and adhere to Can be more lenient with structure and tag
Valid Content
strict syntax rules. closing.

Mainly used for storing data and data


Data Handling Mainly used for presenting data to users.
exchange.

Example of XML:

xml

Copy code

<book>

<title>Learning XML</title>

<author>John Doe</author>

<price>29.99</price>

</book>

Example of HTML:

html
Copy code

<!DOCTYPE html>

<html>

<head>

<title>Learning HTML</title>

</head>

<body>

<h1>Welcome to HTML</h1>

<p>This is an example of HTML.</p>

</body>

</html>

8. Explain why XML is often referred to as a self-descriptive language. With code example.

Why XML is Self-Descriptive:

XML is called a self-descriptive language because the tags used within an XML document describe
the data contained within them. The element names are meaningful and allow you to understand
the data even without additional documentation.

Example:

xml

Copy code

<person>

<name>John Doe</name>

<age>30</age>

<address>123 Main St, Anytown, USA</address>

</person>

Explanation:

 The tags <name>, <age>, and <address> are self-descriptive. They clearly explain what data is
being represented (name, age, and address), making the content of the document easier to
understand.

9. Describe the structure of a basic XML document with an example.

Structure of an XML Document:


A basic XML document typically contains:

1. XML Declaration: The first line indicates the XML version (optional but recommended).

2. Root Element: The root element wraps all the data.

3. Child Elements: Tags that define data.

4. Attributes (optional): Provide additional information about elements.

Example:

xml

Copy code

<?xml version="1.0" encoding="UTF-8"?>

<library>

<book>

<title>XML for Beginners</title>

<author>Jane Doe</author>

<price>19.99</price>

</book>

<book>

<title>Advanced XML</title>

<author>John Smith</author>

<price>29.99</price>

</book>

</library>

Explanation:

 The <?xml ... ?> declaration specifies the XML version.

 The <library> element is the root element containing <book> elements as children, each with
its own nested elements (<title>, <author>, and <price>).

10. How do you define attributes in XML? Provide an example.

Defining Attributes in XML:

Attributes are defined within opening tags of elements to provide additional information about the
element. They are written as key-value pairs.

Example:

xml
Copy code

<book title="XML for Beginners" author="Jane Doe" price="19.99"/>

Explanation:

 In the example above, title, author, and price are attributes of the <book> element, providing
extra information about the book.

11. What is an XML parser, and why is it necessary for processing XML documents?

XML Parser:

An XML parser is a program or tool that reads XML documents and converts them into a format that
is easier for software to process, usually in the form of a tree structure (DOM) or a series of events
(SAX).

Why It's Necessary:

 Validation: Ensures the document is well-formed and valid according to the XML standard.

 Data Extraction: Helps extract and manipulate data from XML files efficiently.

 Data Interchange: Allows applications to communicate and exchange structured data in a


consistent way.

12. What is the difference between synchronous and asynchronous execution in JavaScript?

Synchronous Execution:

In synchronous execution, operations are executed one after another. The program waits for each
operation to complete before moving to the next one.

 Example:

javascript

Copy code

console.log("Start");

console.log("Middle");

console.log("End");

Output:

sql

Copy code

Start

Middle

End
Asynchronous Execution:

Asynchronous operations allow some operations to run in the background, and the program
continues execution without waiting for the operation to complete. JavaScript handles async tasks
with mechanisms like callbacks, promises, and async/await.

 Example:

javascript

Copy code

console.log("Start");

setTimeout(() => console.log("Middle"), 1000);

console.log("End");

Output:

sql

Copy code

Start

End

Middle

13. Discuss the role of HTML, CSS, and JavaScript in web development, providing examples of how
they work together to build dynamic, interactive websites.

HTML (Hypertext Markup Language):

HTML provides the structure of the web page. It uses elements like <div>, <p>, and <a> to define the
content.

Example:

html

Copy code

<!DOCTYPE html>

<html>

<body>

<h1>Welcome to My Website</h1>

<button id="clickMe">Click Me</button>

</body>

</html>
CSS (Cascading Style Sheets):

CSS is used for styling the HTML structure, specifying things like layout, colors, and fonts.

Example:

css

Copy code

body {

background-color: lightblue;

button {

background-color: green;

color: white;

JavaScript:

JavaScript provides the functionality, allowing users to interact with the page, for example by clicking
buttons or entering data.

Example:

javascript

Copy code

document.getElementById("clickMe").addEventListener("click", () => {

alert("Button clicked!");

});

Explanation:

 HTML creates the structure.

 CSS styles the page.

 JavaScript adds interactivity.

14. Explain the role of media queries in CSS3 and how they contribute to responsive design.

Media Queries in CSS3:

Media queries allow for applying different styles depending on conditions like viewport width,
resolution, and orientation. This is essential for responsive design, ensuring a web page adapts to
different screen sizes and devices.

Example:
css

Copy code

@media (max-width: 600px) {

body {

background-color: lightgray;

Explanation:

 If the viewport width is 600px or less, the background color changes to light gray.

15. Explain the role of the onmouseover() method in event handling with a code example.

onmouseover() Method:

The onmouseover() event occurs when the mouse pointer enters an element. It is commonly used
for hover effects like changing styles when a user interacts with an element.

Example:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<style>

.hoverBox {

width: 100px;

height: 100px;

background-color: lightblue;

</style>

</head>

<body>

<div class="hoverBox" onmouseover="this.style.backgroundColor='yellow'"></div>

</body>
</html>

Explanation:

 The onmouseover event changes the box's background color to yellow when the mouse
pointer hovers over the element.

16. Write a program to handle a click event using JavaScript.

Program Example:

html

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<style>

button {

padding: 10px;

background-color: lightblue;

</style>

</head>

<body>

<button id="clickBtn">Click Me!</button>

<script>

document.getElementById("clickBtn").addEventListener("click", function() {

alert("Button clicked!");

});

</script>

</body>

</html>

Explanation:

 When the button is clicked, an alert appears with the message "Button clicked!"
17. Explain the use of alert() in JavaScript. Provide an example of when it might be useful.

Use of alert():

The alert() method in JavaScript is used to display a simple pop-up message to the user. It's often
used for debugging or giving feedback to users.

Example:

javascript

Copy code

alert("This is an alert message!");

Use Case:
alert() can be useful for notifying users about an error or success during form validation before
submitting data.

Example:

javascript

Copy code

let userInput = "";

if (userInput === "") {

alert("Please fill out the required field.");

Explanation:

 If the user input is empty, the alert() will notify them to fill in the required field.

You might also like