0% found this document useful (0 votes)
11 views

CSS_QB (1)

Css
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CSS_QB (1)

Css
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CSS-Question Bank

1. Define object. Explain the types of objects with example.


JavaScript is an object-based scripting language.
A javascript object is a collection of named values. These named values are referred to as
properties of object.
A JavaScript object is a collection of properties and methods.
A method is a function and property is a value or set of values
1. Native Objects/ Built-in Objects: are those objects supplied by JavaScript.
Example: Math,Date,String
2. Host Objects: are objects that are supplied to JavaScript by the browser environment.
Example: Window
3. User-Defined Objects: are those that are defined by you, the programmer.
Example: person.age

2. Write a JavaScript to demonstrate the use of key events.

<html>
<body>
<input type="text" onkeydown="alert('You have pressed a key inside text input!')">

<input type="text" onkeyup="alert('You have released a key inside text input!')">


<input type="text" id="demo" onkeypress="myFunction()">
<script>
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
<p><strong>Note:</strong> Try to enter some text inside input box and textarea.</p>
</body>
</html>
3. Write a JavaScript to demonstrate the use of onmousemove and onmousedown?
Answer:
<html>
<body>
<p id="p1" onmousedown="mouseDown()">
Click the text! The mouseDown() function is triggered when the mouse button is
pressed down over this paragraph. The function sets the color of the text to red. The
mouseUp() function is triggered when the mouse button is released. The mouseUp()
function sets the color of the text to blue.
</p>
<script>
function mouseDown()
{
document.getElementById("p1").style.color = "red";

}
</script>
</body>
</html>

<html>
<head>
<style>
div {
width: 200px;
height: 100px;
border: 1px solid black;
}
</style>
</head>
<body>
<p>This example demonstrates how to assign an "onmousemove" event to a div element.</p>
<div onmousemove="myFunction(event)"></div>
<p>Mouse over the rectangle above, and get the coordinates of your mouse pointer.</p>
<p id="demo"></p>
<script>
function myFunction(e)
{
var x = e.clientX;
var y = e.clientY;
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("demo").innerHTML = coor;
}
</script>
</body>
</html>

<html>
<head>
<style>
div{ height:200px;
width:100px;
border:1px solid black;
}
</style>
<body>
<div id="demo" onmousemove="mouseDown()">

<script>
function mouseDown() {
document.getElementById("demo").style.backgroundColor="red";
}
</script>
</body>
</html>

4. Write a JavaScript code to perform all arithmetic operations using switch case.
Answer:
<html>
<body>
<script>
let x, y, result, ch;
ch = parseInt(prompt(“Enter your choice:”));

if (ch > 0 && ch < 6) {


x = parseInt(prompt(“Enter first number”));
y = parseInt(prompt(“Enter second number”));
}

switch (ch) {
case 1:
result = x + y;
document.write(“\nResult is :” + result;)
break

case 2:
result = x – y;
document.write(“\nResult is :” + result);
break

case 3:
result = x * y;
document.write(“\nResult is :” + res);
break
case 4:
res = x / y;
document.write(“\nResult is :” + result);
break

case 5:
result = x % y;
document.write(“\nResult is :” + result);
break

default:
document.write(“Invalid Choice:” + ch);
}
</script>

</body>
</html>

5. Write a JavaScript code to find whether entered year leap year or not.
Answer:

<html>
<head>
<script>
function check_leapyear(){

var year;

year = document.getElementById("year").value;

if( (0 == year % 4) && (0 != year % 100) || (0 == year % 400) )


{
alert(year+" is a leap year");
}
else
{
alert(year+" is not a leap year");
}
}
</script>
</head>
<body>

<input type="text" id="year"></input>


<button onclick="check_leapyear()">Check</button>

</body>
</html>

6. Define array with example.


Answer:

 Array is used to store a set of values (different types) in a single variable name. Arrays are the
fundamentals part of the most programming languages and scripting languages. Using array, we
can store multiple values under a single name.
 Syntax: var array_name[item1 , item2]
 var cities = ["London", "Paris", "New York"];

7. List any 4 real time applications of JavaScript.

Answer:
i. Build forms that respond to user input without accessing a server.
ii. Display clocks
iii. Client server validation
iv. Dynamite drop-down menu
v. Displaying date and time
vi. Opening and closing new window or frame
vii. Build forms that respond to user input.
8. Write 2 ways to declare an array with example.

Answer:
a) By using new Array() constructor:
 Call it with no arguments: var a= new Array();
This method is creates an empty array with no elements and is
equivalent to the array literal [].
 Call it with a single numeric argument, which specifies a length:
Var a = new Array(10);
This technique creates an array with specified length.
 Explicitly specify two or more array elements or a single non-numeric
element for the element for the array: Var a = new
Array(5,4,3,2,1,”testing”, “testing”);
In this form, the constructor arguments become the elements of the new
array

b) By using Literal Notation

Instead of new Array(), you can use square brackets [].

When we declare array using square brackets is called the “array literal
notation”:
Var x= [];
Var x= [5];

9. List datatypes in JavaScript.

Answer:
10.Define Event. List 4 types of Mouse events.
Answer:
The change in the state of an object is known as an Event. There are various events in HTML
which represents that some activity is performed by the user or by the browser. When js code is
included in html it reacts over these events and allows the execution. This process of reacting is
known as event handling. Thus, js handles html events via event handler

4 types of event are:

i. onmouseout event
ii. onmouseover event
iii. onwheel event
iv. onmouseup event
v. onmousedown event
vi. ondblclick event

11.Define: Calling function with arguments


Answer:
A function is called by using the function name followed by parentheses. If the function has
arguments, values for each argument are placed within the parentheses.

<html>
<body>
<script>
function IncreaseSalary(OldSalary, PercIncrease)
{
var NewSalary =
OldSalary * (1 + (PercIncrease / 100))
alert("Your new salary is " + NewSalary)
}
var Salary = prompt('Enter old salary.', ' ')
var Increase =
prompt('Enter salary increase as percent.', ' ')
IncreaseSalary(Salary, Increase)
</script>
</body>
</html>

12.Write the use of following methods with example:


a) substr( ) b) parseFloat( ) c) Number() d) parseInt()

Answer:

a) substr(): It is used to fetch the part of the given string on the basis of the specified
starting position and length.

Eg: output - Javasc

<html>
<body>

<script type="text/javascript">
var str = "Javascript";
document.write(str.substr(0,6));
</script>

</body>
</html>
b) parseFloat( ): The parseFloat() method is used similarly to the parseInt() method, except
the parseFloat() method is used with any number that has a decimal value.

Example:

<html>
<body>
<script type="text/javascript">
var price = '10.95';
var converted = parseFloat(price);
document.write(converted);
</script>
</body>
</html>

c) Number():converts a string into number.

Example:

<html>
<head>
<title>Number() Function </title>
</head>
<body>
<script type="text/javascript">
var a = '49';
var b = '1';
var c = Number(a) + Number(b);

document.write(c);
</script>

</body>
</html>

d) parseInt():converts a number in a string to an integer numeric value, which is a whole


number.

Example:

<html>

<head>
<title>NUmber() Function </title>
</head>
<body>
<script type="text/javascript">
var a = "49";
var digit = parseInt(a);
document.write(digit);
</script>

</body>
</html>

<html>

<body>

<script>

var str = 'javascript';

document.write(str.substr(0,6));

var str1 = '10.59';

document.write(parseFloat(str1));

var str2 = '1' ;

var str3 = '49';

document.write(Number(str2)+Number(str3));

var str4 = '60';

var num = parseInt(str4);

document.write(num)

</script>

</body>

</html>

13.Write a simple JavaScript program to join all elements of the following array into a string.
Sample array : myColor = ["Red", "Green", "White", "Black"];
Expected Output :
"Red,Green,White,Black"
"Red,Green,White,Black"
"Red+Green+White+Black"
Answer:
<html>
<body>
<script>
var color = ["Red", "Green", "White", "Black"];
var text = color.join();
var text1=color.join(";");
var text2=color.join("+");

document.write(text +"</br>");
document.write(text1 +"</br>");
document.write(text2);
</script>

</body>
</html>

14.Write a JavaScript program to sort the items of an array.


Sample array : var arr1 = [ 3, 8, 7, 6, 5, -4, 3, 2, 1 ];
Sample Output : -4,-3,1,2,3,5,6,7,8
Answer:
<html>
<body>
<script>
var arr1 = [ 3, 8, 7, 6, 5, -4, 3, 2, 1 ];
document.write(arr1.sort());
</script>

</body>
</html>

15.Write a JavaScript program to find the sum of squares of a numeric vector.


Answer:
<!DOCTYPE html>
<html>
<body>
<script>
function square(array) {
var sum = 0,
i = array.length;
while (i--)
sum += Math.pow(array[i], 2);
return sum;
}

document.write(square([9,5,7,8]));
</script>
</body>
</html>

16.Write a JavaScript program to compute the sum and product of an array of integers.
Answer:

<!DOCTYPE html>
<html>
<body>
<script>
var array = [4,5,8,9,7],
s = 0,
p = 1,
i;
for (i = 0; i < array.length; i += 1)
{
s += array[i];
p *= array[i];
}
document.write('Sum : '+s + ' Product : ' +p);
</script>

</body>
</html>
17.Define objects and write different ways to create an Object with example.
Answer:
Objects are collection of properties and methods.
 A Methods is a function that is a member of an object.
 A Property is a value or set of values that is the member of an object.
i. Define and create a single object, using an object literal.
Using an object literal, you both define and create an object in one statement.
Example:
var person = {
firstName: “Hhh",
lastName: “Bbb",
age: 10
};

ii. Define and create a single object, with the keyword “new” OR by creating instance of
Object
new keyword is used to create object.
Syntax: var objectname=new Object();
Example:
var person = new Object();
person.firstName = “Hhh";
person.age = 10;

iii. Define an object constructor, and then create objects of the constructed type.
Example:
function person(firstName, lastName, age)
{
this. firstName = firstName;
this. lastName = lastName;
this. age = age;
}
p=new person(“aaa”,”vvv”,10);
document.write(p.firstName+" "+p.lastName+" "+p.age);
18.How to declare string in JavaScript? Explain its with example.
Answer:
A) By string literal:

The string literal is created using double quotes.

Syntax:
var stringname="string value";
var name = “Rohan”

B) By string object (using new keyword)

Syntax:
var stringname=new String("string literal");

Example:
var name =new String("Rohan ");

19.What is an event? Explain onClick event handler with example.


Answer:
The change in the state of an object is known as an Event.
Example:
<html>
<head><title>Javascript Form Events : onClick Event</title>
<script>
function Japan()
{

alert("konnichiwa");
}
function India()
{
alert("namaste");
}
function Germany()
{
alert("Guten Tag");
}
</script>
</head>
<body>
<p>Hello in Different Countries</p>
<form>
<input type="button" value="Japan" onclick ="Japan()" />

<input type="button" value="India" onclick ="India()" />

<input type="button" value="Germany" onclick ="Germany()" />


</form>
</body>
</html>

20.Why form validation is required in JavaScript?


Answer:

i. IT is important to validate the form submitted by the user because it can have inappropriate
values.
ii. So, validation is must to authenticate user.
iii. JavaScript provides facility to validate the form on client server side validation.
iv. Through, JavaScript we can validate
 Name
 Password
 Email
 Date
 Mobile number n many more

21.List any 4 features of JavaScript.


Answer:
i. JavaScript is an object-based scripting language.
ii. Giving the user more control over the browser.
iii. It Handling dates and time.
iv. It is light weighted.
v. JavaScript is a scripting language and it is not java.
vi. JavaScript is case sensitive.
vii. Every statement in JavaScript must be terminated with semicolon (;).

22.Write the use of following String methods: a) charCodeAt() b) fromCharCode()


Answer:

a) charCodeAt(): It provides the Unicode value of a character present at the specified index.
Var Unicode = StringName.charCodeAt()

b) fromCharCode(): The fromCharCode() method converts Unicode values into characters


var res = String.fromCharCode(72, 69, 76, 76, 79);

You might also like