Study resource

Read at your pace, then save it for later.

Unit 3: Web Technology DHTML, JavaScript, and Form Validation Exam Questions

Section A — 1 Mark Questions

1. What is a scripting language? [1 Mark]

Answer: A scripting language is a programming language used to write scripts that automate tasks and control the behaviour of applications or webpages. Example: JavaScript.

2. What is the full form of CSS? [1 Mark]

Answer: CSS stands for Cascading Style Sheets. It is used to style and design HTML webpages.

3. What do you mean by form validation? [1 Mark]

Answer: Form validation is the process of checking user-entered data to ensure that it is correct, complete, and follows the required format before submission.

4. How do you declare a variable in JavaScript? [1 Mark]

Answer: Variables in JavaScript can be declared using the var, let, or const keywords.

Example:

let age = 20;

5. What is the purpose of JavaScript "onClick" event? [1 Mark]

Answer: The onclick event is used to execute a JavaScript function or code when a user clicks on an HTML element, such as a button or link.

Section B — 2 Mark Questions

1. Write a simple JavaScript program to print a message "hello" world. [2 Marks]

Answer: The following JavaScript program displays the message "Hello World" using the document.write() method.

Program:

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
</head>
<body>

    <script>
        document.write("Hello World");
    </script>

</body>
</html>

Output:

Hello World

Explanation: The <script> tag is used to write JavaScript code inside an HTML document. The document.write() method displays the specified message on the webpage.

2. What is JavaScript function? How can a function be called from an HTML element using an event? [2 Marks]

Answer:

JavaScript Function: A JavaScript function is a reusable block of code designed to perform a particular task. It executes when it is called or invoked.

Calling a Function Using an HTML Event: A JavaScript function can be called from an HTML element by using an event attribute such as onclick. When the specified event occurs, the associated function is executed.

Example:

<!DOCTYPE html>
<html>
<body>

    <button onclick="showMessage()">
        Click Me
    </button>

    <script>
        function showMessage() {
            alert("Hello World!");
        }
    </script>

</body>
</html>

Explanation: When the user clicks the button, the onclick event calls the showMessage() function, which displays an alert message containing "Hello World!".

Section C — 8 Mark Questions

1. Discuss the different features of DHTML. [6 Marks]

Answer:

Introduction

DHTML (Dynamic HyperText Markup Language) is a combination of HTML, CSS, JavaScript, and the Document Object Model (DOM) used to create interactive and dynamic webpages. It allows webpage content, appearance, and behaviour to change without requiring the browser to load an entirely new page for every change.

Features of DHTML

  1. Dynamic Content:

    DHTML allows webpage content to change dynamically according to user actions or other conditions. Text, images, and other elements can be modified without reloading the entire webpage.

  2. Dynamic Styling:

    It allows the appearance of webpage elements to change dynamically. Using CSS and JavaScript, developers can modify colours, fonts, sizes, backgrounds, and other visual properties.

  3. Event Handling:

    DHTML supports events such as mouse clicks, mouse movements, keyboard input, and form submission. JavaScript event handlers are used to perform specific actions when these events occur.

  4. Animation and Effects:

    DHTML can create animations and visual effects such as moving objects, changing images, hiding and showing elements, and interactive menus.

  5. Interactive Webpages:

    It helps developers create interactive webpages that respond to user actions. For example, dropdown menus, image changes, interactive buttons, and form validation.

  6. DOM Manipulation:

    DHTML uses the Document Object Model (DOM) to access and modify HTML elements dynamically. JavaScript can add, remove, or change webpage elements and their content.

Example of DHTML

The following example demonstrates how JavaScript can dynamically change the content of an HTML element.

<!DOCTYPE html>
<html>
<head>
    <title>DHTML Example</title>
</head>
<body>

    <h2 id="message">Welcome to HTML</h2>

    <button onclick="changeText()">
        Click Me
    </button>

    <script>
        function changeText() {
            document.getElementById("message").innerHTML =
                "Welcome to DHTML!";
        }
    </script>

</body>
</html>

Explanation: Initially, the webpage displays "Welcome to HTML". When the user clicks the button, JavaScript uses the DOM method getElementById() to find the heading and changes its content to "Welcome to DHTML!" without reloading the webpage.

Conclusion

DHTML makes webpages more interactive, attractive, and user-friendly by combining HTML, CSS, JavaScript, and DOM. It is useful for developing dynamic menus, animations, interactive forms, and other responsive webpage features.

Difference between HTML and DHTML [2 Marks]

Answer: HTML is used to create and structure the content of webpages, whereas DHTML combines HTML, CSS, JavaScript, and DOM to make webpages dynamic and interactive.

HTML DHTML
HTML stands for HyperText Markup Language. DHTML stands for Dynamic HyperText Markup Language.
It is mainly used to create static webpage structures. It is used to create dynamic and interactive webpages.
Content generally changes when the page is reloaded or updated. Content and styles can change dynamically without reloading the entire page.
It mainly defines the structure of a webpage. It combines HTML, CSS, JavaScript, and DOM.

2. Write a program in JavaScript to find the largest of three numbers. [4 Marks]

Answer:

JavaScript Program to Find the Largest of Three Numbers

The following program takes three numbers as input from the user and compares them using conditional statements to find the largest number.

Program:

<!DOCTYPE html>
<html>
<head>
    <title>Largest of Three Numbers</title>
</head>
<body>

    <script>
        let a = Number(prompt("Enter first number:"));
        let b = Number(prompt("Enter second number:"));
        let c = Number(prompt("Enter third number:"));

        if (a >= b && a >= c) {
            document.write("Largest number is: " + a);
        }
        else if (b >= a && b >= c) {
            document.write("Largest number is: " + b);
        }
        else {
            document.write("Largest number is: " + c);
        }
    </script>

</body>
</html>

Explanation

  1. Three numbers are accepted from the user using the prompt() function.
  2. The Number() function converts the entered values into numbers.
  3. The if condition checks whether the first number is greater than or equal to the other two numbers.
  4. The else if condition checks whether the second number is the largest.
  5. Otherwise, the third number is displayed as the largest.

Sample Input:

First number: 25
Second number: 40
Third number: 15

Output:

Largest number is: 40

3. Write a program in JavaScript to find the factorial of an integer number. [4 Marks]

Answer:

JavaScript Program to Find the Factorial of a Number

The factorial of a non-negative integer is the product of all positive integers from 1 to that number. It is represented by the symbol (!).

Formula:

n! = n × (n − 1) × (n − 2) × ... × 2 × 1

Example: 5! = 5 × 4 × 3 × 2 × 1 = 120

Also, 0! = 1.

Program

<!DOCTYPE html>
<html>
<head>
    <title>Factorial of a Number</title>
</head>
<body>

    <script>
        let n = Number(prompt("Enter an integer number:"));
        let fact = 1;

        if (!Number.isInteger(n) || n < 0) {
            document.write("Please enter a non-negative integer.");
        }
        else {
            for (let i = 1; i <= n; i++) {
                fact = fact * i;
            }

            document.write("Factorial of " + n + " is: " + fact);
        }
    </script>

</body>
</html>

Explanation

  1. The program accepts an integer number from the user using prompt().
  2. A variable named fact is initialized with 1.
  3. The program checks whether the entered value is a non-negative integer.
  4. A for loop runs from 1 to the entered number.
  5. In each iteration, the value of i is multiplied by fact and stored back in fact.
  6. After the loop finishes, the factorial result is displayed on the webpage.

Sample Input:

Enter an integer number: 5

Output:

Factorial of 5 is: 120

Conclusion

JavaScript uses variables, loops, conditional statements, and arithmetic operators to perform calculations. The above program demonstrates how a for loop can be used to calculate the factorial of a given non-negative integer.


xxx

Did this help you understand better?

Your feedback improves the quality of this resource for everyone.