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

CSS_QB

The document is a comprehensive question bank on JavaScript covering various topics such as objects, arrays, events, and string methods. It includes definitions, examples, and code snippets for practical understanding. Additionally, it addresses form validation and key features of JavaScript.

Uploaded by

nebuladreamerx
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

The document is a comprehensive question bank on JavaScript covering various topics such as objects, arrays, events, and string methods. It includes definitions, examples, and code snippets for practical understanding. Additionally, it addresses form validation and key features of JavaScript.

Uploaded by

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

CSS-Question Bank

1. Define object. Explain the types of objects 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.
Types of Objects:
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:
var person =
{
firstname: "Girija",
lastname: "Jore",
age: 10
};

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

3. <html>
4. <body>
5. <input type="text" name="" id="" onkeypress="alert('Key
pressed')">
6. <input type="text" name="" id="" onkeydown="alert('Key
down')">
7. <input type="text" name="" id="" onkeyup="alert('Key
released')">
8. <script>
9.
10. </script>
11. </body>
12. </html>

3. Write a JavaScript to demonstrate the use of onmousemove and onmousedown?


Answer:
<html>

<body>
<div id="i1" style="border: 2px solid black;width:300px;height:
300px;" onmousemove="mouseMove(event)">
<p>onmousemove event
</p>
<p id="coords"></p>
</div>

<label for="">onmousedown</label>
<input type="text" onmousedown="alert('you pressed down mouse key')">

<script>
let container = document.getElementById("i1")
function mouseMove(e) {
document.getElementById("coords").innerText = `x: $
{e.clientX}, y: ${e.clientY}`
}
</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 :” + result);
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"];

 An array is a type of object used for storing multiple values in single
variable.
 Each value (also called an element) in an array has a numeric position,
known as its index, and it may contain data of any data type-numbers,
strings, Booleans, functions, objects, and even other arrays.
 The array index starts from 0, so that the first array element is arr [0].
 The simplest way to create an array is by specifying the array
elements as a comma-separated list enclosed by square brackets, as
shown in the example below:
 var cities = ["London", "Paris", "New York"];
 alert(cities[2]); // Output: New York
 var a = ["London", 500, ”aa56”, 5.6];

i. List any 4 real time applications of JavaScript.

Answer:
ii. Build forms that respond to user input without accessing a server.
iii. Display clocks
iv. Client server validation
v. Dynamic drop-down menu
vi. Displaying date and time
vii. Opening and closing new window or frame
viii. Web applications

ix. Build small but complete client-side programs.

x. Displaying popup windows and dialog boxes (like alert dialog


box, confirm dialog box and prompt dialog box)

7. 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];

8. List datatypes in JavaScript.

Answer:
9. Define Event. List 3 types of keybord events.

Answer:
The change in the state of an object is known as an Event.
4 types of event are:
1. onkeypress
2. onkeydown
3. onkeyup

10. 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. The sequence of arguments
should be same as listed in the function definition.A comma must separate each value.

<html>
<body>
<script>
function add(num1, num2)
{
var sum =
num1 + num2
alert("Sum is " + sum)
}
var num1 = parseInt(prompt('Enter num 1’))
var num2 = parseInt(prompt('Enter num 2’))
add(num1, num2)
</script>
</body>
</html>

11. Write the use of following methods with example:


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

Answer:

a) substr():The substr() method returns a portion of the string, starting at


the specified index and extending for a given number of characters
afterwards

Eg:

<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.

parseFloat() method parses an argument and returns a floating point number

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>
12. 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>

13. 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>

14. 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;
i--
}

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

15. 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],
let s = 0,
let p = 1,
let i;
for (i = 0; i < array.length; i += 1)
{
s = s+ array[i];
p = p* array[i];
}
document.write('Sum : '+s + ' Product : ' +p);
</script>

</body>
</html>

16. 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);

17. 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 ");

18. 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>

19. 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

20. 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 (;).

21. 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.

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

You might also like