Section A — 1 Mark Questions
1. What is JavaScript? [1 Mark]
Answer: JavaScript is a lightweight, cross-platform, interpreted scripting language primarily used for client-side web development.
2. Is JavaScript a compiled or interpreted language? [1 Mark]
Answer: JavaScript is an interpreted language.
3. What is the primary purpose of client-side scripting? [1 Mark]
Answer: It is used to add interactivity and dynamic behavior to web pages within the user's browser.
4. Define a variable in JavaScript. [1 Mark]
Answer: A variable is a symbolic name for a data container used to store information values.
5. Which keyword is commonly used to declare a variable in modern JavaScript? [1 Mark]
Answer: The let or const keywords are used for variable declaration.
6. What is a function in JavaScript? [1 Mark]
Answer: A function is a block of reusable code designed to perform a particular task.
7. What is the function of an 'if' statement? [1 Mark]
Answer: It allows conditional execution of code blocks based on whether a specified condition is true or false.
8. What is the purpose of a loop in JavaScript? [1 Mark]
Answer: Loops are used to execute a block of code repeatedly as long as a specified condition remains true.
9. Name one arithmetic operator in JavaScript. [1 Mark]
Answer: The addition operator (+).
10. What is the purpose of web form validation? [1 Mark]
Answer: It ensures that the data entered by a user into a form meets specific criteria before submission.
11. What does the acronym DHTML stand for? [1 Mark]
Answer: DHTML stands for Dynamic HyperText Markup Language.
12. What are the three components that make up DHTML? [1 Mark]
Answer: HTML, CSS, and JavaScript.
13. Which component of DHTML is responsible for the visual styling of elements? [1 Mark]
Answer: CSS (Cascading Style Sheets).
14. What is an 'event' in the context of web development? [1 Mark]
Answer: An event is an action or occurrence recognized by the software, such as a mouse click or a key press.
15. What is the purpose of a button in DHTML? [1 Mark]
Answer: Buttons are used to trigger JavaScript functions when clicked by the user.
16. Can DHTML change the content of a page after it has loaded? [1 Mark]
Answer: Yes, it can change content dynamically.
17. Which browser feature can be controlled using JavaScript in DHTML? [1 Mark]
Answer: Browser properties like window size, location, and history.
18. What is the output of 5 + '5' in JavaScript? [1 Mark]
Answer: '55'.
19. Does JavaScript require a server to execute client-side code? [1 Mark]
Answer: No, it executes in the client's browser.
20. What is a logical operator in JavaScript? [1 Mark]
Answer: An operator that returns a boolean value, such as AND (&&) or OR (||).
Section B — 2/3 Mark Questions
1. Differentiate between a variable and a function. [2 Marks]
Answer: A variable acts as a container for storing data values, whereas a function is a block of code designed to perform a specific action or calculation when called.
2. Explain the role of CSS in DHTML. [2 Marks]
Answer: CSS is responsible for defining the layout, colors, fonts, and positioning of elements, which JavaScript can then dynamically modify to create DHTML effects.
3. List three common JavaScript events. [3 Marks]
Answer: 1. onclick (triggered when a user clicks an element), 2. onmouseover (triggered when a mouse pointer moves over an element), 3. onload (triggered when an object has loaded).
4. Why is form validation necessary? [2 Marks]
Answer: Validation is necessary to ensure user input is accurate and complete, which reduces server load and improves the user experience by providing immediate feedback.
5. Describe the function of a 'for' loop with a small example. [3 Marks]
Answer: A for loop repeats a block of code a set number of times. Example: 'for (let i = 0; i < 5; i++)' will execute the loop body five times, incrementing i each time.
6. How does DHTML combine HTML and JavaScript? [2 Marks]
Answer: HTML provides the structural elements (tags), while JavaScript acts upon these elements by modifying their properties or CSS styles in response to user actions.
7. Briefly explain the 'if-else' conditional statement. [2 Marks]
Answer: The if-else statement executes one block of code if a condition is true, and an alternative block of code if the condition is false.
8. What are the primary features of DHTML? [3 Marks]
Answer: 1. Ability to update page content without reloading, 2. Interactive user interfaces, 3. Capability to animate elements and control layout dynamically.
9. How do logical operators work in JavaScript? [2 Marks]
Answer: They evaluate expressions and return a boolean (true or false). For example, the AND (&&) operator returns true only if both operands are true.
10. Mention two ways JavaScript can control the browser. [2 Marks]
Answer: JavaScript can open new browser windows and navigate the user to a different URL via the window.location object.
Section C — 5 Mark Questions
1. Describe the role of JavaScript as a client-side scripting language. [5 Marks]
Answer:
Introduction
JavaScript is a versatile, high-level, interpreted programming language that serves as the core of client-side web development.
Main Explanation
As a client-side language, JavaScript runs within the user's web browser rather than on a remote server. This allows it to interact directly with the web page displayed to the user.
- Interactivity: It enables dynamic response to user actions like clicking buttons or entering text in forms.
- Document Manipulation: It can modify the HTML and CSS of a page on-the-fly, allowing for dynamic visual changes.
- Validation: It processes input data locally, saving server bandwidth and time.
- Browser Control: It can respond to browser events, handle cookies, and control page navigation.
By executing locally, JavaScript provides a responsive and seamless experience for web users.
2. Explain the concept of DHTML and its primary components. [5 Marks]
Answer:
Definition
DHTML (Dynamic HyperText Markup Language) is a collection of technologies used to create interactive and animated websites.
Components
- HTML: Provides the structural foundation and the elements of the document.
- CSS: Controls the visual presentation, positioning, and style of the elements.
- JavaScript: Acts as the 'engine' that connects HTML and CSS, allowing the developer to change styles or structure in real-time based on events.
Working Principle
In DHTML, JavaScript can target specific HTML elements and alter their CSS properties. For example, by changing the 'visibility' property of a div via JavaScript, an element can be hidden or shown. This combination allows for menus, animations, and dynamic forms that react to user inputs without requiring a full page refresh from the server.
3. Discuss the importance of Web Forms and Validations in JavaScript. [5 Marks]
Answer:
Introduction
Web forms are the standard interface for users to submit data to a website. Validation is the essential process of checking this data.
Why Validation is Necessary
- Data Integrity: It ensures that the information received is in the correct format, such as a valid email address or numeric age.
- User Experience: By catching errors (like empty fields) instantly on the client-side, users receive immediate feedback without waiting for a server round-trip.
- Efficiency: It reduces unnecessary traffic to the server by ensuring only valid data is sent for processing.
How it Works
JavaScript uses event listeners (like 'onsubmit') to trigger validation functions. These functions examine the value of form inputs and return a boolean. If the input is invalid, the script can display an error message and cancel the form submission, preventing corrupt data from being processed.
4. Elaborate on the usage of Operators, Conditions, and Loops in JavaScript. [5 Marks]
Answer:
Operators
JavaScript uses various operators to perform operations on data. Arithmetic operators (+, -, *, /) perform math, while logical operators (&&, ||, !) are used to combine multiple conditions. Assignment operators (=, +=) are used to store values.
Conditions
Conditions provide decision-making capabilities. The if...else construct checks a boolean value and runs code accordingly. This is critical for logic-heavy scripts, such as verifying user credentials.
Loops
Loops provide a way to repeat an action. A 'for' loop is best when the number of iterations is known, using a counter variable. A 'while' loop is useful when the repetition depends on a specific state. Together, these control structures allow scripts to handle complex data processing tasks efficiently, such as iterating over a list of items to generate dynamic HTML content.
5. Explain how events and buttons are used to control the browser in DHTML. [5 Marks]
Answer:
Introduction
Events and buttons form the interactive layer of a webpage, allowing the user to drive the browser's behavior.
Events
An event is a signal that something has happened, such as a user click (onclick) or a mouse movement (onmouseover). Developers write event handlers, which are JavaScript functions that 'listen' for these signals.
Buttons and Triggering
Buttons are the most common HTML elements used to initiate these events. By assigning an event handler to a button's 'onclick' attribute, developers can execute specific JavaScript functions.
Browser Control
Through these handlers, JavaScript can manipulate the browser environment. Examples include:
- Opening new windows or tabs via window.open().
- Redirecting the user to a new URL using window.location.href.
- Alerting the user with messages using the alert() function.
- Manipulating the document history or changing the current page title.
This integration of buttons and events transforms a static document into a functional, user-driven application.