CSS Model Answers (Sample Question Paper)
CSS Model Answers (Sample Question Paper)
Aditi Gharat
console.log(user.name); // Output: John
// Calling a method of an object
let user = {
name: 'John',
age: 28,
sayHi() {
console.log(`Hi, my name is ${this.name}`);
}
};
user.sayHi(); // Output: Hi, my name is John
1. && (AND): The && operator is used to evaluate if both expressions are true.
The expression will only return true if both expressions are true.
Aditi Gharat
c) Write a JavaScript that identifies a running browser.
Answer:
<html>
<body>
<script >
// Get the user agent string
var userAgentString = navigator.userAgent;
// Detect Chrome
if (userAgentString.indexOf("Chrome") > -1) {
alert("You are using Chrome!");
}
// Detect Firefox
else if (userAgentString.indexOf("Firefox") > -1) {
alert("You are using Firefox!");
}
// Detect Safari
else if (userAgentString.indexOf("Safari") > -1) {
alert("You are using Safari!");
}
// Detect Edge
else if (userAgentString.indexOf("Edge") > -1) {
alert("You are using Edge!");
}
// Detect Opera
Aditi Gharat
else if (userAgentString.indexOf("Opera") > -1) {
alert("You are using Opera!");
}
// Unknown
else {
alert("We could not detect which browser you are using!");
}
</script>
</body>
</html>
d) Write a JavaScript that initializes an array called “Fruits” with names of five
fruits. The script then displays the array in a message box.
Answer:
<html>
<head>
<script type="text/javascript">
var Fruits = ["Apple", "Banana", "Orange", "Kiwi", "Strawberry"];
alert("Fruits: " + Fruits);
</script>
</head>
</html>
Aditi Gharat
e) Give syntax of and explain the use of “with” statement/clause in JavaScript
using suitable example.
Answer:
Syntax:
with (object) {
// code
}
Example:
var person = {
name: "John Doe",
age: 25
};
with (person) {
console.log(name + " is " + age + " years old.");
}
f) Enlist and explain the use of any two Intrinsic JavaScript functions.
Answer:
Intrinsic functions are built-in functions that are part of the JavaScript language,
and are available to all JavaScript programs. They are used to perform common
tasks such as string manipulation, mathematical calculations, and other
operations.
Aditi Gharat
1) parseInt() :- The parseInt() function is used to convert a string into an integer
value. It takes a string as an argument and returns an integer if the string is valid,
otherwise it returns NaN (not a number). This function is useful when working
with numbers that are stored as strings in variables.
Aditi Gharat
Q.2) Attempt any THREE of the following. 12 Marks
a) Write syntax of and explain prompt method in JavaScript with the help of
suitable example.
Answer:
Syntax:
window.prompt( message, default);
Example:
<html>
<head>
<script type="text/javascript">
function promptUser(){
var userName = window.prompt("Please enter your name", "Harry
Potter");
document.write("Welcome " + userName);
}
</script>
</head>
<body>
<input type="button" onclick="promptUser()" value="Prompt Box" />
</body>
</html>
Explanation:
The prompt() method in JavaScript displays a dialog box that prompts the user for
input. It takes two parameters; the message to be displayed, and a default value.
In the above example, when the user clicks on the “Prompt Box” button, the
window.prompt method is called which displays a dialog box asking the user to
Aditi Gharat
enter their name. The message displayed is "Please enter your name" and the
default value is "Harry Potter". The user can enter any value, which is then stored
in the userName variable.
Answer:
<html>
<body>
<script>
var student = ["Advait", "Anay", "Manyata", "Saanvi", "Saachi"];
Aditi Gharat
var total = 0;
for(var i=0; i < marks.length; i++) {
total += marks[i];
}
Aditi Gharat
c) Write a JavaScript that displays all properties of window object. Explain the
code.
Answer:
<html>
<head>
<script type="text/javascript">
var prop;
for (prop in window) {
document.write(prop + " : " + window[prop] + "<br>");
}
</script>
</head>
<body>
</body>
</html>
Explanation: This JavaScript code will loop through all the properties of the
window object and display them on the page. It starts by declaring the variable
prop. Then, it loops through all the properties of the window object. For each
property, it will write the property name and the value of the property on the
page.
<body>
<script>
function isPalindrome(str) {
Aditi Gharat
var reverseString = str.split('').reverse().join('');
if (reverseString === str) {
return true;
} else {
return false;
}
}
var inputString = prompt("Please enter a string to check if it is palindrome or
not");
if (isPalindrome(inputString)) {
alert("The entered string is a palindrome!");
} else {
alert("The entered string is not a palindrome!");
}
</script>
</body>
</html>
Aditi Gharat
4. The concat() method does not 4. The join() method takes an
take any argument and returns a optional argument which is the
new array with the elements of the separator between the elements
original array combined. of the array.
5.Eg: 5.Eg:
var str = cars.concat() var str = cars.join(' ')
The value of str is 'BMW, Audi, The value of str in this case is 'BMW
Maruti' Audi Maruti'
b) Write a JavaScript function to count the number of vowels in a given string.
Answer:
<html>
<head>
<title>Vowel Counter</title>
<script>
function vowel_count(str) {
let count = 0;
const vowels = ["a", "e", "i", "o", "u"];
Aditi Gharat
</body>
</html>
Aditi Gharat
d) Write a function that prompts the user for a color and uses what they select
to set the background color of the new webpage opened .
Answer:
<html>
<head>
<title>Change Background Color</title>
<script type="text/javascript">
function setBackground() {
var color = prompt("Please enter a background color","");
if (color != null) {
document.body.style.background = color;
}
}
</script>
</head>
<body>
<button onclick="setBackground()">Change Background Color</button>
</body>
</html>
Aditi Gharat
Q.4) Attempt any THREE of the following. 12 Marks
a) State what is a regular expression? Explain its meaning with the help of a
suitable example.
Answer:
A regular expression is a sequence of characters that define a search pattern. It is
mainly used for string pattern matching. Regular expressions are very powerful
and can be used to perform a wide variety of string manipulations.
For example, the following HTML code counts the number of words in a given
sentence:
<html>
<head>
<script>
function wordCount() {
// Get sentence from text area
let sentence = document.getElementById("input").value;
Aditi Gharat
</body>
</html>
In the above example, the regex /\w+/g is used to create a search pattern that
looks for any word in the given sentence. The words are then counted and
displayed as an alert.
b) Write a webpage that accepts Username and adharcard as input texts. When
the user enters adhaarcard number ,the JavaScript validates card number and
diplays whether card number is valid or not. (Assume valid adhaar card format
to be nnnn.nnnn.nnnn or nnnn-nnnn-nnnn).
Answer:
<!DOCTYPE html>
<html>
<head>
<title>Adhaar Card Validation</title>
<script>
function validateForm() {
var adhaarCard = document.forms["myForm"]["adhaarCard"].value;
if (adhaarCard.length != 14) {
alert("Adhaar Card Number Length should be 14");
return false;
}
var regex = /^\d{4}\-\d{4}\-\d{4}$/;
if (regex.test(adhaarCard) == false) {
alert("Adhaar Card Number should be in format nnnn.nnnn.nnnn or
nnnn-nnnn-nnnn");
return false;
}
}
</script>
</head>
<body>
Aditi Gharat
<form name="myForm" action="" onsubmit="return validateForm()"
method="post">
<div>
<label>Username: </label>
<input type="text" name="username">
</div>
<div>
<label>Adhaar Card Number: </label>
<input type="text" name="adhaarCard">
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
c) Write the syntax of and explain use of following methods of JavaScript Timing
Event.
a. setTimeout()
b. setInterval()
Answer:
a. setTimeout():
The setTimeout() method calls a function or evaluates an expression after a
specified number of milliseconds.
Syntax:
setTimeout(function, milliseconds, param1, param2, ...)
Example:
setTimeout(function(){ alert("Hello") }, 3000);
Aditi Gharat
b. setInterval():
The setInterval() method calls a function or evaluates an expression at specified
intervals (in milliseconds).
Syntax:
setInterval(function, milliseconds, param1, param2, ...)
Example:
setInterval(function(){ alert("Hello") }, 3000);
d) Develop JavaScript to convert the given character to Unicode and vice versa.
Answer:
<html>
<head>
<script type="text/javascript">
function charToUnicode()
{
var text = document.getElementById("text").value;
var result="";
for(var i=0; i<text.length; i++)
{
result += "\\u" + text.charCodeAt(i).toString(16);
}
document.getElementById("result").value=result;
}
function unicodeToChar()
{
var code = document.getElementById("code").value;
var result = "";
for(var i=0; i<code.length; i+=4)
{
result += String.fromCharCode(parseInt(code.substr(i, 4), 16));
Aditi Gharat
}
document.getElementById("result2").value=result;
}
</script>
</head>
<body>
<h1>Character to Unicode Converter</h1>
<input type="text" id="text" />
<input type="button" value="Convert" onclick="charToUnicode()" />
<input type="text" id="result" />
<h1>Unicode to Character Converter</h1>
<input type="text" id="code" />
<input type="button" value="Convert" onclick="unicodeToChar()" />
<input type="text" id="result2" />
</body>
</html>
e) List ways of Protecting your webpage and describe any one of them.
Answer:
Ways to Protect a Webpage:
1. Use Secure Passwords: Utilizing strong and secure passwords is one of the most
effective ways to protect a webpage. Passwords should be complex and contain a
combination of upper and lowercase letters, numbers, and special characters.
2. Implement Two-Factor Authentication: Two-factor authentication (2FA) is a
security protocol that requires users to provide two pieces of authentication
information to access a particular resource. This can include a combination of a
username and password, as well as something unique to the user, such as a one-
time PIN.
3. Utilize SSL Encryption: SSL (Secure Socket Layer) is an encryption protocol that
provides secure communication between two devices or networks. It encrypts
Aditi Gharat
data that is sent over the network, so even if it is intercepted, the data cannot be
read.
4. Use Firewalls: A firewall is a system that prevents unauthorized access to or
from a private network. It monitors incoming and outgoing network traffic and
blocks suspicious activity or malicious traffic.
5. Monitor Access Logs: Access logs are a record of all the times someone has
accessed a webpage. By monitoring access logs, administrators can quickly spot
any suspicious activity and take appropriate action.
Example:
Using Secure Passwords: Secure passwords are an important part of protecting a
webpage. Passwords should be complex and contain a combination of upper and
lowercase letters, numbers, and special characters. It is important to not use the
same password for multiple accounts and to change passwords periodically.
Additionally, it is recommended to use a password manager to store passwords
securely and make them easier to remember.
Aditi Gharat
Q.5) Attempt any TWO of the following. 12 Marks
a) Write HTML Script that displays textboxes for accepting Name, middlename,
Surname of the user and a Submit button. Write proper JavaScript such that
when the user clicks on submit button
i) all texboxes must get disabled and change the color to “RED”. and with
respective labels.
ii) Constructs the mailID as <name>.<surname>@msbte.com and displays mailID
as message. (Ex. If user enters Rajni as name and Pathak as surname mailID will
be constructed as [email protected]) .
Answer:
<html>
<head>
<script>
function Submit(){
document.getElementById("name").disabled = "true";
document.getElementById("middlename").disabled = "true";
document.getElementById("surname").disabled = "true";
document.getElementById("name").style.backgroundColor="red";
document.getElementById("middlename").style.backgroundColor="red";
document.getElementById("surname").style.backgroundColor="red";
Aditi Gharat
<form>
<label>Name</label><input type="text" id="name">
<br>
<label>Middle Name</label><input type="text" id="middlename">
<br>
<label>Surname</label><input type="text" id="surname">
<br>
<input type="button" value="Submit" onclick="Submit()">
</form>
</body>
</html>
b) Write a webpage that diplays a form that contains an input for Username and
password. User is prompted to enter the input and password and password
becomes value of the cookie. Write The JavaScript function for storing the
cookie . It gets executed when the password changes.
Answer:
<html>
<head>
<title>Form for Username and Password</title>
</head>
<body>
<form>
Username: <input type="text" name="username" id="username" />
<br />
Password: <input type="password" name="password" id="password" />
<br />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
function storeCookie(){
var username = document.getElementById("username").value;
Aditi Gharat
var password = document.getElementById("password").value;
var expDate = new Date();
expDate.setTime(expDate.getTime() + (1 * 24 * 60 * 60 * 1000));
var expires = "expires="+ expDate.toUTCString();
document.cookie = "username=" + username + "; " + expires;
document.cookie = "password=" + password + "; " + expires;
}
document.getElementById("password").onchange = storeCookie;
</script>
</body>
</html>
FRAME2 FRAME3
FRUITS
FLOWERS
CITIES
Fruits, Flowers and Cities are links to the webpage fruits.html, flowers.html,
cities.html respectively. When these links are clicked corresponding data
appears in “FRAME3”.
Answer:
<html>
<head>
<title>Frame Demo</title>
</head>
<body>
<table border="1">
Aditi Gharat
<tr>
<td align="center" colspan="2">
FRAME 1
</td>
</tr>
<tr>
<td>
FRAME 2
<ul>
<li>
<a href="fruits.html" target="mainframe">FRUITS</a>
</li>
<li>
<a href="flowers.html" target="mainframe">FLOWERS</a>
</li>
<li>
<a href="cities.html" target="mainframe">CITIES</a>
</li>
</ul>
</td>
<td>
FRAME 3<BR>
<iframe name="mainframe"></iframe>
</td>
</tr>
</table>
</body>
</html>
Aditi Gharat
Q.6) Attempt any TWO of the following. 12 Marks
Aditi Gharat
</head>
<body>
<h1>Select a City</h1>
<select onchange="showDescription(this.value)">
<option value="NewDelhi">New Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Bangalore">Bangalore</option>
</select>
<br><br>
<table>
<tr>
<td>Description:</td>
<td id="cityDescription"></td>
</tr>
<tr>
<td>Image:</td>
<td><img id="cityImage" width="200" height="200"></td>
</tr>
</table>
</body>
</html>
b) Develop a JavaScript Program to Create Rotating Banner Ads with URL Links.
Answer:
<html>
<head>
<script>
Aditi Gharat
var thisAd = 0;
function rotate(){
if(thisAd == ad_images.length-1){
thisAd = 0;
}else {
thisAd++;
}
document.getElementById("adBanner").src=ad_images[thisAd];
}
window.setInterval(rotate, 3000);
</script>
</head>
<body>
</body>
</html>
Aditi Gharat
c) Create a slideshow with the group of four images, also simulate the next and
previous transition between slides in your JavaScript.
Answer:
<html>
<head>
<script>
pics = new Array('1.jpg' , '2.jpg' , '3.jpg', '4.jpg');
count = 0;
function slideshow(status)
{
if (document.images)
{
count = count + status;
if (count > (pics.length - 1))
{
count = 0;
}
if (count < 0)
{
count = pics.length - 1;
}
documet.imag1.src = pics[count];
}
}
</script>
</head>
<body>
<img src="1.jpg" width="200" name="img1">
<br>
<input type="button" value="Next" onclick="slideshow(1)">
<input type="button" value="Back" onclick="slideshow(-1)">
</body>
</html>
Aditi Gharat