CSS Solved QB 22-23-1
CSS Solved QB 22-23-1
2 Marks Questions
Chapter – I
1. Enlist Features of JavaScript.
a. JavaScript is a object-based scripting language.
b. Giving the user more control over the browser.
c. It Handling dates and time.
d. It Detecting the user's browser and OS
e. It is light weighted.
f. Client – Side Technology
g. JavaScript is a scripting language and it is not java.
h. JavaScript is interpreter based scripting language.
i. JavaScript is case sensitive.
j. JavaScript is object based language as it provides predefined objects.
k. Every statement in javascript must be terminated with semicolon (;).
l. Most of the javascript control statements syntax is same as syntax of control statements
in C language.
m. An important part of JavaScript is the ability to create new functions within scripts.
Declare a function in JavaScript using function keyword
Property:
• It is the value associated with each object.
• Each object has its own set of properties.
• For example, a form object has a title, a width, and a height—to mention a few properties
Method:
• A method is a process performed by an object when it receives a message.
• For example, a Submit button on a form is an object. Its Submit label and the dimensions of
the button are properties of the button object.
• If you click the Submit button, the form is submitted to the server-side application. In other
words, clicking the Submit button causes the button to process a method.
Main Event:
• Event causes JavaScript to execute the code.
• In JavaScript when user submits form, clicks button, writes something to text box the
corresponding event gets triggered and execution of appropriate code is done through
Event Handling.
Chapter – III
10. List the various form events.
Event Event
Description
Performed Handler
<script type="text/javascript">
function getcube()
{
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
4 Marks Questions
Chapter – I
12. Write a JavaScript code to design a form to accept values for username and
password.
<html>
<body>
<form name="login">
Enter Username<input type="text" name="userid"><br>
Enter Password<input type="password" name="pswrd">
<input type="button" onclick="display()" value="Display">
</form>
<script language="javascript"> function display()
{
document.write("User ID "+ login.userid.value +
"Password : "+login.pswrd.value);
}
</script>
</body>
</html>
13. Explain prompt and confirm method of JavaScript with syntax and example
prompt()
The prompt () method displays a dialog box that prompts the visitor for input.
The prompt () method returns the input value if the user clicks "OK". If the user clicks
"cancel" the method returns null.
Syntax:
window.prompt (text, defaultText)
Example:
<html>
<script type="text/javascript">
function msg()
{
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="button" value="click" onclick="msg()"/>
</html>
confirm()
It displays the confirm dialog box. It has message with ok and cancel buttons.
Returns Boolean indicating which button was pressed
Syntax:
window.confirm("sometext");
Example :
<html>
<script type="text/javascript">
function msg()
{
var v= confirm("Are u sure?"); if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="button" value="delete record" onclick="msg()"/>
</html>
14. Write a JavaScript program which computes average marks of the student and
according to average marks determine the grade
<html>
<head>
<title>Compute the average marks and grade</title>
</head>
<body>
<script>
var students = [['Summit', 80], ['Kalpesh', 77], ['Amit', 88],
['Tejas', 93],['Abhishek', 65]];
var Avgmarks = 0;
for (var i=0; i < students.length; i++)
{
Avgmarks += students[i][1];
}
var avg = (Avgmarks/students.length);
document.write("Average grade: " + (Avgmarks)/students.length);
document.write("<br>");
if (avg < 60)
{
document.write("Grade : E");
}
else if (avg < 70)
{
document.write("Grade : D");
}
else if (avg < 80)
{
document.write("Grade : C");
}
else if (avg < 90)
{
document.write("Grade : B");
}
else if (avg < 100)
{
document.write("Grade : A");
}
</script>
</body>
</html>
<html>
<head>
<title>Functions</title>
<body>
<script language="Javascript">
var myCar =
{
/* Data properties */
defColor: "blue",
defMake: "Toyota",
/* Accessor properties (getters) */
get color()
{
return this.defColor;
},
get make()
{
return this.defMake;
},
/* Accessor properties (setters) */
set color(newColor)
{
this.defColor = newColor;
},
set make(newMake)
{
this.defMake = newMake;
}
};
document.write("Car color:" + myCar.color + " Car Make:
"+myCar.make)
/* Calling the setter accessor properties */
myCar.color = "red";
myCar.make = "Audi";
/* Checking the new values with the getter accessor properties */
document.write("<p>Car color:" + myCar.color); // red
document.write(" Car Make: "+myCar.make); //Audi
</script>
</head>
</body>
</html>
Chapter – II
16. Write the use of charAt() method and indexOf() method with syntax and example.
charAt()
• The charAt() method requires one argument i.e is the index of the character that you
want to copy.
Syntax:
var SingleCharacter = NameOfStringObject.charAt(index);
Example:
var FirstName = 'Bob';
var Character = FirstName.charAt(0); //o/p B
indexOf()
• The indexOf() method returns the index of the character passed to it as an
argument. If the character is not in the string, this method returns –1.
•
Syntax:
var indexValue = string.indexOf('character');
Example:
var FirstName = 'Bob';
var IndexValue = FirstName.indexOf('o'); //o/p index as 1
17. Difference between concat() and join() method of array with example.
concat() join()
Array elements can be combined by using Array elements can be combined by using
concat() method of Array object. join() method of Array object.
The concat() method separates each value The join() method also uses a comma to
with a comma. separate values, but you can specify a
character other than a comma to separate
values.
Eg: Eg:
var str = cars.concat() var str = cars.join(' ')
The value of str is The value of str in this case is
'BMW, Audi, Maruti' 'BMW Audi Maruti'
18. Write a JavaScript that will replace following specified value with another value in the
string String=”I will fail” replace “fail” by “pass”.
<html>
<head>
<body>
<script>
var myStr = ‘I will fail’;
var newStr = myStr.replace(fail, "pass");
document.write(newStr);
</script>
</body>
</head>
</html>
21. Write a HTML script which displays 2 radio buttons to the users for fruits and
vegetables and 1 option list. When user select fruits radio button list should present
only fruits to the user & when user select vegetables radio button option list should
present only vegetables names to the user.
<html>
<head>
<title>HTML Form</title>
<script language="javascript" type="text/javascript">
function updateList(ElementValue)
{
with(document.forms.myform)
{
if(ElementValue == 1)
{
optionList[0].text="Mango";
optionList[0].value=1;
optionList[1].text="Banana";
optionList[1].value=2;
optionList[2].text="Apple";
optionList[2].value=3;
}
if(ElementValue == 2)
{
optionList[0].text="Potato";
optionList[0].value=1;
optionList[1].text="Cabbage";
optionList[1].value=2;
optionList[2].text="Onion";
optionList[2].value=3;
}
}
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
<p>
<select name="optionList" size="2">
<option value=1>Mango
<option value=2>Banana
<option value=3>Apple
</select>
<br>
<input type="radio" name="grp1" value=1 checked="true"
onclick="updateList(this.value)">Fruits
<input type="radio" name="grp1" value=2
onclick="updateList(this.value)">Vegetables
<br>
<input name="Reset" value="Reset" type="reset">
</p>
</form>
</body>
</html>
22. Describe how to evaluate checkbox selection. Explain with suitable example.
Evaluating Checkbox Selection:
• A checkbox is created by using the input element with the
type=”checkbox” attribute-value pair.
• A checkbox in a form has only two states(checked or un-checked) and is
independent of the state of other checkboxes in the form.
• Check boxes can be grouped together under a common name.
• You can write JavaScript function that evaluates whether or not a check box was
selected and then processes the result according to the needs of your application.
• Following example make use of five checkboxes to provide five options to the user
regarding fruit.
<html>
<head>
<title>HTML Form</title>
<script language="javascript" type="text/javascript">
function selection()
{
var x ="You selected: ";
with(document.forms.myform)
{
if(a.checked == true)
{
x+= a.value+ " ";
}
if(b.checked == true)
{
x+= b.value+ " ";
}
if(o.checked == true)
{
x+= o.value+ " ";
}
if(p.checked == true)
{
x+= p.value+ " ";
}
if(g.checked == true)
{
x+= g.value+ " ";
}
document.write(x);
}
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
Select Your Favourite Fruits: <br>
<input type="checkbox" name="a" value="Apple">Apple
<input type="checkbox" name="b" value="Banana">Banana
<input type="checkbox" name="o" value="Orange">Orange
<input type="checkbox" name="p" value="Pear">Pear
<input type="checkbox" name="g" value="Grapes">Grapes
<input type="reset" value="Show" onclick="selection()">
</form>
</body>
</html>
</form>
</body>
</html>