Javascript Borad Paper Questions
Javascript Borad Paper Questions
2M
Sunil has written following code to store subjects in an array and display the total number of subjects in
the array. His code is not running properly. Identify and underline the syntax errors in the following
code and write the corrected code:
function countsubs( )
{
Var Subject = [“Eng” , “Math”, “Science” , “Hindi”];
Document.write (subject.length)
}
Ans:
function countsubs( )
{
var Subject = [“Eng” , “Math”, “Science” , “Hindi”];
document.write(Subject.length);
}
3M
Explain the following functions with respect to JavaScript:
a) Substring
b) Concat()
c) Shift()
Ans :
a) Substring : Extracts the characters from a string, between two specified indices
b) Concat() : Joins two or more strings, and returns a copy to the joined strings
c) Shift() : Removes the first element of an array and returns that element
Write a function in JavaScript that accept three numbers as arguments and display the greatest number
Ans :
function greatest(n1, n2, n3)
{ if (n1>n2)
If (n1>n3)
alert(“greatest number is : “+ n1);
else
document.write(“greatest number is : “ + n3);
else if ( n2>n3)
document.write(“greatest number is : “ + n2);
else
document.write(“greatest number is : “ + n3);
}
4M
Write a program in JavaScript to
- Accept first name and last name from the user.
- Display number of characters in his first name
- Display current date, his name as initial letter of first name and complete last name ( hint: it should
display K. Kumar for name Krishan Kumar)
Solution :
<html>
<body>
<script >
f=prompt("enter first name ");
l=prompt("enter last name ");
d= new Date ();
document.write(“ today’s date : “, d);
document.write("length of first name is ", f.length);
document.write("\n Your name is ",f[0]+". " +l);
</script>
</body>
</html>
a)Write a program in JavaScript to display a week day randomly from the array of days of week given
below:
dow=[“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday” ] (2)
b) Write code in JavaScript to accept a string from the user and display the first occurrence of word
“good” in the entered string.
Ans :
<html><body>
<script >
n=Math.ceil(Math.random()*7)-1
dow=[“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday” ]
document.write( " Day ", dow[n] );
</script></body></html>
b) Write code in JavaScript to accept a string from the user and display the first occurrence of word
“good” in the entered string. (2)
Ans :
<html>
<body>
<script >
str=prompt(" enter a string : ");
document.write(str.match("good"));
document.write("<br>", str.indexOf("good"));
</script></body></html>
Ans :
value after right shift is : 5 value after left shift is : 80
b) Write code snippets in JavaScript to accept height of 5 students in an array and display the tallest
and the smallest height of student.
Ans :
<html>
<body>
<script >
var i = 0;
str=new Array();
while (i<5)
{
str[i]=parseInt(prompt(" enter the height : "));
i++;
}
str.sort();
document.write(“smallest “ , str[0]);
document.write(“Tallest “, str[4]);
</script></body></html>
3m
Explain the following with respect to Java script using suitable example.
1. Event handler
2. Objects
3. Operator
Ans :
1. Event Handler - The "event handler" is a command that is used to specify actions in
response to an event. Eg: onLoad, onMouseover etc.
2. Objects: JavaScript objects are simply collections of name-value pairs. The "name"
part is a JavaScript string, while the value can be any JavaScript value including
more objects. Eg: string object, math object etc
or
JavaScript supports programming with objects. Objects are a way of organizing the
variables. The different screen elements such as Web pages, forms, text boxes,
images, and buttons are treated as objects.
3. Operator: Operators are symbols which perform some operation on values.
JavaScript operators can be used to perform various operations such as:
Arithmetic Operators
Comparison Operators
Logical Operators
Relational Operators
Assignment Operators
Conditional Operators
sum += arguments[i];
}
return sum / arguments.length;
5M
Write a code in Java Script to accept a string from the user, the program should:
- display length of the string
- print the string in upper case
- print the first two characters of the string
- print the string in reverse order
Ans :
x= prompt("enter a string ");
l=x.len();
document.write(“ length of the string is : “ +l);
document.write(“ String in Uppercase is :” + x. toUpperCase());
document.write(“ First two characters of string are” + x.substring(0,2));
document.write(“ Reverse of the string is :” +x.reverse());
( 1 mark for accepting the string
1 mark for displaying the length
1 mark for converting the string into upper case
1 mark for displaying the substring
1 mark for displaying reverse of string)
Write the code in JavaScript to accept a number from the user. It should display whether the number
entered by the user is even or odd. The code should display an alert box also if the number is less than
or equal to zero.
Ans :
<script>
var num;
num= prompt( enter a number”);
n=parseInt(num);
if(n<=0)
alert(“ the number is less than or equal to zero”);
if( n%2= =0)
document.write(“ number is even “);
else
document.write(“ number is odd “);
(
</script>
Ans : substring( ) : Extracts the characters from a string, between two specified indices.
reverse() Reverses the order of an element in an array
Eg : var str = “good morning” ;
var ss = str.substring (1,4 ) ;
var days =[“Mon”, “ Tue” , “ Wed”, “Thu”];
days.reverse(); // will reverse the elements as “Thu , Wed, Tue, Mon
5m
Write code for OK button on a web page to find length of the name entered in the
textbox using prompt window:
Ans : <html>
<head>
<script>
function myFunction()
{ var str;
str = prompt( “enter your name”);
document.write(“the length of the string is “” + str.length);
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="OK"/>
</body>
</html>
2M
1.Write the output of the given code :
<html>
<body>
<script>
s= ["Have","a","nice","day"]
s.reverse();
for (i=0;i<s.length;i++)
document.write(s[i]);
</script>
</body> </html>
2.Compare properties and methods of an object w.r.t. JavaScript objects.
3.Write the names of two parameters that HTML calculates to download.
3M
Predict the output of the following codes in JavaScript : 3
(i) document.write(parseInt("101",2);
(ii) document.write(parseInt("101",8);
(iii) document.write(parseInt("101",10);
35. Write a code in JavaScript to accept a string from the user and display first occurrence of the word ‘‘the’’ in
it.
3M
What will be the final value of A and B on executing the following code : 3
A = 43
A++
B = A+1
5m
Write a function that displays a prompt box which accepts a name and displays a greet message ‘‘hello
<name>, Have a good day’’ when the user enters a name in a <p>(paragraph) which has an id ‘‘demo’’. The
function header is as below : 5
function myfunc()
{
}
<html>
<body>
<p id="demo"></p>
<script>
function myfunc()
{
var z=prompt("Enter the name");
return ("hello "+z+", have a good day");
}document.getElementById("demo").innerHTML=myfunc();
</script></body></html>
Sqp
2m
Differentiate between Client side JavaScript and Server side JavaScript.
17. What will be the output of the following ? 2
A=‘‘Be positive’’
(a) var d = A.indexOf(‘‘positive’’)
(b) document.write(A.length)
3m
Assuming variable A holds 25 and B holds 2, identify the type of operator
and write the output of the following : 3
(a) (B--)
(b) (A! = B)
24. Define an Event. Write any two commonly used Events.
5m
Write JavaScript code to : 5
(a) Display a confirm box with the message ‘‘Do you want to continue ?’’.
(b) Create an array X.
(c) Store ‘‘Top’’, ‘‘Bottom’’ in the array X.
(d) Store Date and Time in variable B.
(e) Print the message ‘‘Welcome’’ on a web page.
2019 sqp
2m
Differentiate between ‘‘%’’ and ‘‘/ ’’ operator with the help of an example.
What will be the output of the following, if a = 1 ? Explain.
switch(a)
{ case 1:
case 2: document.write(‘‘Hello’’);
break;
}
3m
Assuming variable A holds 20 and B holds 5. Identify the operator and write the output of the following :
(a) (A&&B)
(b) (A * B)
24. Define Data type. Which data type will you use to store the following : 3
(a) Name of a person
(b) True/False
5m
Write a function that displays a confirm box with the message ‘‘Do you want to continue ?’’ and displays a
message ‘‘Thanks’’ when the user presses OK otherwise in a <p>(paragraph) which has an id ‘‘demo’’. The
function header is as below :
function myfunc()
{
}
Note : When OK is pressed on confirm box it returns a value true.
Differentiate between ‘‘=’’ and ‘‘==’’ operators with the help of an example.
(j) What will be the output of the following, if a=1 ? Explain.
switch(a)
{ case1:
case2: document.write(‘‘Hello’’);
break;
}
What is a Function ? Write a user defined function to display an alert box displaying ‘‘Hard work is the key
to success’’.
3m
Assuming variable A holds 20 and variable B holds 5, identify the operator and write the output of the following :
(i) (A&&B)
(ii) (A*B)
(e) Define Data type. Which data type will you use to store the following :
(i) Name of a person
(ii) True/false
5m
Write a JavaScript code to perform the following on an array N containing the following : [‘‘Sincerity’’,‘‘Honesty’’]
(i) Add ‘‘Patience’’ to the array N.
(ii) Join array N to another array M.
(iii) Delete the last element from the array M.
(iv) Sort the array.
(v) Display the array in reverse.
2018 set 4
2m
Use Object Tag to embed MP3 file into the site with the following specification;
Source = "music.wav"
type ="Audio/mpeg"
(h) Explain the AUTOSTART attribute of <EMBED> tag.
(i) Differentiate between Client side Javascript and Server side Javascript.
(j) What will be the output of the following ?
A="Hardwork pays"
(i) var d = A.indexOf("pays")
(ii) document.write(A.length)
3m
Assuming variable A holds 10 and B holds 2. Identify the type of operator and
write the output of the following :
(i) (B--)
(ii) (A!=B)
(e) Define an Event. Write any two commonly used Events with their description.
5m
Write JavaScript code to :
(i) Display a confirm box with the message "Do you want to continue ?"
(ii) Create an array X
(iii) Store "Home", ''Next" in the array X
(iv) Store Date and Time in variable B
(v) Print the message "Welcome "in a web-page.
(b) Write any five features of JavaScript
5M
Write a script to accept five names from user, store them in an array and
display the sorted array. 5
41. (a) Write the output of the given code : 5
nm="Fruit";
m= nm.length;
switch(m)
{ case 2: document.write("two");
case 3: document.write("three");
case 4: document.write("four");
case 5: document.write("five");
case 6: document.write("six");
case 7: document.write("seven");
}
Ans:five,six,seven
3M
Explain any three attributes of object tag. 3
34. Give and explain any three applications of JavaScript. 3
35. Write a code in JavaScript that accepts a binary number from the user
and displays its decimal equivalent number. 3
Ans
<html
<body>
<script>
document.write(parseInt("100101",2));
</script>
</body>
</html>
3M
37. Write a code in JavaScript to accept five numbers from user, store them
in an array, and display the sorted array.
5M
Write a code in JavaScript to accept a number from the user and
generate 5
(a) a random number greater than the number entered.
(b) next even number after the number entered.
41. Write a program in JavaScript to accept marks in three subjects, display
the total marks obtained, the percentage, and the grade as per the norms
given below : 5
Percentage Grade
>=75 A
<75 and >=60 B
<60 and >=33 C
<33 D