COLLEGE OF INFORMATICS AND VIRTUAL EDUCATIONACADEMIC YEAR 2024/2025
SECTION A: (40 MARKS)
Attempt ALL questions in this section
Question One
Fill in the blanks with the correct answer.
(1 Mark Each)
a. In HTML, the tag used to create a hyperlink is called ______.
Answer (Click to show)
<a>
b. The jQuery method used to retrieve the value of an input field is called ______.
Answer (Click to show)
.val()
c. To create a list that does not have numbers, you use the ______ tag.
Answer (Click to show)
<ul>
d. To center a block element, you can set its margins to ______.
Answer (Click to show)
auto
e. A ______ is a collection of properties and methods in JavaScript.
Answer (Click to show)
object
f. To make a responsive design, you can use ______ queries.
Answer (Click to show)
media
g. In JavaScript, the ______ operator checks for both value and type equality.
Answer (Click to show)
** === (strict equality)**
h. To create an animation in jQuery, you can use the ______ method.
Answer (Click to show)
.animate()
i. The ______ function in JavaScript can be used to loop through arrays.
Answer (Click to show)
for loop (or .forEach() method)
j. The ______ method is used to remove an element from the Document Object Model (DOM) in jQuery.
Answer (Click to show)
.remove()
Question Two
a. Outline three (3) places where you can put your JavaScript code during creation of a web page. (3 Marks)
Answer (Click to show)
Inline: Within the HTML file, inside <script> tags.
Internal: Within the HTML file, inside <script> tags in the <head> or <body> sections.
External: In a separate .js file, linked to the HTML document using the <script src="file.js"> tag.
b. In most websites, HTML, CSS and JavaScript are normally used. Explain each and emphasising on the purpose of their use in a web page. (6 Marks)
Answer (Click to show)
HTML (HyperText Markup Language): It is the standard markup language used to create the structure and content of a web page. It defines elements like headings, paragraphs, links, images, and forms. Its purpose is to give the page its fundamental skeleton and meaning.
CSS (Cascading Style Sheets): It is a stylesheet language used to describe the presentation of a document written in HTML. It controls the layout, colors, fonts, and overall visual style of the web page. Its purpose is to make the web page aesthetically pleasing and to control its visual design across different devices.
JavaScript: It is a programming language that enables interactive and dynamic content on web pages. It can be used to manipulate the HTML and CSS, handle user events (like clicks), validate forms, create animations, and communicate with servers. Its purpose is to make web pages responsive and engaging for the user.
c. Outline three (3) ways in which CSS can be integrated into a web page. (3 Marks)
Answer (Click to show)
Inline CSS: Using the style attribute directly within an HTML element.
Internal CSS: Using the <style> tag within the <head> section of the HTML document.
External CSS: Linking to an external .css file using the <link> tag within the <head> section of the HTML document.
d. Write a JavaScript code that displays the message in Figure 1 and gives an alert if a user clicks a button (click me). Feel free to put any message on the alert. (3 Marks)
Answer (Click to show)
<!-- HTML --><button onclick="showAlert()">Click Me</button><script> // JavaScript function showAlert() { alert("Button was clicked! Thank you."); }</script>
Question Three
Write true for the correct and false for the incorrect statement.
(1 Mark Each)
a. In jQuery, $(document).ready() is used to run code when the DOM is fully loaded.
Answer (Click to show)
True
b. In CSS, the margin property controls the space inside an element.
Answer (Click to show)
False (The margin controls the space outside an element; padding controls the space inside.)
c. In JavaScript, undefined is a data type.
Answer (Click to show)
True
d. The <canvas> element is used for drawing graphics on the web page.
Answer (Click to show)
True
e. jQuery can be used to create AJAX applications.
Answer (Click to show)
True
f. The float property can be used to create multi-column layouts.
Answer (Click to show)
True
g. jQuery is a replacement for JavaScript.
Answer (Click to show)
False (jQuery is a library built with JavaScript, not a replacement for it.)
h. Pseudo-classes are used to define the state of an element, such as :hover.
Answer (Click to show)
True
i. Regular expressions (Regex) can be used for string validation in JavaScript.
Answer (Click to show)
True
j. The serialize() method is used to convert form data into a URL-encoded string.
Answer (Click to show)
True
Question Four
Choose the most correct answer and write its letter in the answer booklet provided.
(1 Mark Each)
i. What does SEO stand for?
A. Standardized Encoding Optimization
B. Search Engine Optimization
C. Semantic Element Organization
D. Simple Element Operation
Answer (Click to show)
B. Search Engine Optimization
ii. What is the purpose of the doctype declaration in HTML?
A. To define the character set
B. To specify the HTML version being used
C. To link CSS files
D. To include JavaScript
Answer (Click to show)
B. To specify the HTML version being used
iii. Which selector would you use to target an element with a specific class?
A. #className
B. .className
C. element.className
D. className
Answer (Click to show)
B. .className
iv. Which of the following correctly represents an object in JavaScript?
A. {name: “John”, age: 30}
B. [“John”, 30]
C. “name: John, age: 30”
D. name = “John”, age = 30
Answer (Click to show)
A. {name: “John”, age: 30}
v. What will the following code return? Math.random()?
A. A random integer
B. A random float between 0 and 1
C. A random float between 1 and 10
D. A fixed number
Answer (Click to show)
B. A random float between 0 and 1
SECTION B: (60 MARKS)
Attempt any THREE (3) out of FOUR (4) questions provided.
Question Five
You have been tasked with creating a web application designed to collect information about participants interested in joining a community gardening initiative. This application will gather essential details for each gardener, including their first name, surname, email address, and phone number.
During the development process, the client requested an additional feature to capture information about the gardener’s plot. Specifically, the application should determine if the plot is for a single type of plant or for multiple types. If the plot is for a single type of plant, the application must capture the type of plant and the expected yield. Conversely, if the plot is for multiple types of plants, the application should allow the user to specify the number of different plants and, for each type, record the type of plant and the expected yield.
You are required to use HTML and jQuery to design a dynamic form that adjusts based on the user’s input regarding the number of different plants. For example, if the number of plants is three, the form should display three sets of input fields for the type of plant and expected yield.
(20 Marks)
Answer Guidance (Click to show)
Solution Outline:
HTML Structure:
Create form fields for First Name, Surname, Email, Phone.
Include a radio button or dropdown to select plot type: “Single Plant” or “Multiple Plants”.
For “Single Plant”: Provide one set of inputs for Plant Type and Expected Yield.
For “Multiple Plants”: Provide a number input field for “Number of Plants”.
Include a container <div id="plant-container"></div> where the dynamic inputs will be generated.
jQuery Logic:
Use a change event handler on the plot type selector.
If “Multiple Plants” is selected, show the “Number of Plants” input field.
Use a change event handler on the “Number of Plants” input.
On change, read the value n and use a loop (e.g., for loop) to generate n sets of input fields (for Plant Type and Expected Yield) inside the #plant-container.
Ensure that when the user changes the number again, the previous dynamic fields are cleared (using .empty()) before generating new ones.
If “Single Plant” is selected, hide the “Number of Plants” input and clear the #plant-container, while showing the single set of inputs.
Dynamic Form Sections - Shows different inputs based on single/multiple plant selection
Gardener Information Collection - First name, surname, email, and phone number
Plot Type Selection - Radio buttons for single or multiple plant types
Dynamic Input Generation - Creates plant input fields based on user-specified count
jQuery Integration - Handles all dynamic behavior and form interactions
Responsive Design - Clean, user-friendly interface with proper styling
Question Six
You have been hired by a local event organiser to develop a simple Event Registration Page for an upcoming tech seminar. The page should allow users to enter their full name, email address, and select their preferred session (either “Morning” or “Afternoon”) using a drop-down list. You are required to use HTML to structure the form, including appropriate input fields and a submit button. Using CSS, style the form so that it is centred on the page, with clear spacing, borders around input fields, and a submit button that changes colour when hovered over. Implement JavaScript validation to ensure that all fields are filled and that the email address is in a valid format. If validation fails, display an appropriate error message below the form. Using jQuery, enhance the functionality so that when the form is successfully submitted, it hides the form and displays a personalised thank-you message including the user’s name and selected session. Apply a fade-in animation to the thank-you message using jQuery effects. You may organize your code using inline or external styles and scripts as needed.
(20 Marks)
Answer Guidance (Click to show)
Solution Outline:
HTML:
Create a <form id="registration-form"> with inputs for name (text), email (email), session (select with options “Morning” and “Afternoon”), and a submit button.
Include a <div id="error-message"></div> for displaying errors.
Include a hidden <div id="thank-you-message"></div> for the success message.
CSS:
Center the form using margin: auto and set a fixed width.
Style inputs with padding, margin, and border.
Use the :hover pseudo-class on the submit button to change its background-color.
JavaScript/jQuery:
Attach a submit event handler to the form using jQuery.
Validation: Inside the handler, prevent the default form submission. Check if name and session are not empty. Validate the email using a regular expression (e.g., /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/). If any check fails, display an error in the error-messagediv andreturn false;`.
Success Action: If validation passes, use $('#registration-form').hide(); to hide the form.
Use $('#thank-you-message').html("Thank you, " + name + "! You are registered for the " + session + " session."); to set the personalized message.
Use $('#thank-you-message').fadeIn(); to animate the thank-you message.
You are tasked with designing a simple online shopping cart interface for a local bookstore’s website. The page should display a list of three books, each with a title, price, and an “Add to Cart” button. Using HTML, structure the book list and include appropriate elements to display the information. With CSS, make the list visually appealing by adding borders, spacing, and hover effects on the buttons. Using JavaScript, maintain a count of how many items have been added to the cart and the total price. Each time a user clicks “Add to Cart”, the item should be counted and the total updated. Display the current item count and total amount dynamically on the page. With jQuery, implement a feature that highlights the newly added book for a few seconds using an animation (such as fading or sliding). Additionally, include a “Clear Cart” button that resets the count and total, and use jQuery to animate the clearing action.
(20 Marks)
Answer Guidance (Click to show)
Solution Outline:
HTML:
Create a list of books, each in a container (e.g., <div class="book">) with elements for title, price, and an “Add to Cart” button with a data-price attribute storing the book’s price.
Include a cart summary area (e.g., <div id="cart">) with elements to display Item Count: <span id="item-count">0</span> and Total: $<span id="total-amount">0.00</span>.
Include a “Clear Cart” button.
CSS:
Style .book with border, padding, and margin.
Style the “Add to Cart” button with a :hover effect.
Create a CSS class for highlighting, e.g., .highlight { background-color: yellow; }.
JavaScript/jQuery:
Variables: Use global variables (or a simple object) to track itemCount and totalPrice.
Add to Cart: Attach a click handler to all “Add to Cart” buttons.
On click, get the price from the button’s data-price attribute.
Increment itemCount and add the price to totalPrice.
Update the text of #item-count and #total-amount spans.
Use $(this).closest('.book') to select the book container and add the highlight class. Use setTimeout to remove the class after a few seconds. Alternatively, use $(...).fadeTo(100, 0.5).fadeTo(100, 1) for a flash effect.
Clear Cart: Attach a click handler to the “Clear Cart” button.
Reset itemCount and totalPrice to zero and update the display.
Use an animation like $("#cart").fadeOut(300).fadeIn(300); to provide visual feedback.
You have been hired by a small indie game studio to create a web-based mini-game as part of an online campaign to promote their upcoming product. The client is looking for a simple yet interactive browser game that can engage users for a short period of time. Your task is to design and develop a basic “Whack-a-Mole” game using HTML, CSS, and jQuery.
The game should feature a 3x3 grid, totaling nine holes, where a “mole” randomly appears. Use HTML to create the structure of the grid and the necessary interface elements, such as buttons and score display. CSS should be used to style the game board, the mole, and include animations for visual appeal. The gameplay involves the mole appearing randomly in different holes every 1 to 2 seconds. When a user clicks on the mole, their score should increase by one. jQuery must be used to control the timing of the mole’s appearance, animate interactions, and handle click events to update the score.
Additionally, include a visible score counter and a countdown timer (for example, 30 seconds) on the screen. When the timer runs out, the game should display the final score and a “Play Again” button to restart the game. For an optional challenge, you can add difficulty levels (Easy, Medium, Hard) that modify the mole’s speed, and include sound effects when the mole is hit.
You are expected to submit a fully functional HTML page with either embedded or linked CSS and jQuery. The game should offer a user-friendly interface and smooth gameplay experience. Ensure that your code is well-commented to explain the main logic and functionalities.
(20 Marks)
Answer Guidance (Click to show)
Solution Outline:
HTML:
Create a 3x3 grid using a container <div id="game-board"> with 9 <div class="hole"> elements inside.
Include a score display: Score: <span id="score">0</span>.
Include a timer display: Time Left: <span id="timer">30</span>s.
Include a “Start Game” / “Play Again” button.
CSS:
Style #game-board as a grid using CSS Grid or Flexbox.
Style .hole with a circle shape and a background color.
Create a class .mole with a different background image or color to represent the mole. Add a simple CSS animation (like a small scale-up) when the mole appears.
Start Game: On button click, reset score and timeLeft, update displays, and start intervals.
Timer: Use setInterval to decrement timeLeft every second. When it reaches 0, clear all intervals, hide the mole, and show the final score and “Play Again” button.
Mole Logic: Use another setInterval (with a random delay between 1000-2000ms).
In this interval, remove the .mole class from all holes.
Pick a random hole from the 9 and add the .mole class to it.
Whack!: Attach a click event handler to all holes. If the clicked hole has the .mole class, increment the score, update the display, and remove the .mole class (to represent the mole being whacked).
Difficulty: Modify the range of the random delay in the mole interval based on the selected difficulty (e.g., Easy: 1500-2000ms, Hard: 500-1000ms).
Animation/Sound: Use jQuery’s .fadeIn()/.fadeOut() or add/remove CSS classes for hit animations. Use the HTML5 Audio object to play a sound on a successful hit.