0% found this document useful (0 votes)
6K views

Javascript Borad Paper Questions

The document provides sample questions from past SQP papers on JavaScript concepts and coding questions. It includes questions on JavaScript functions, operators, data types, arrays, strings and objects. Code snippets are provided to demonstrate concepts like parsing numbers to different bases, reversing arrays, extracting substrings and checking for occurrences of a word in a string. Predicted outputs and explanations are given for code examples. Overall, the document covers fundamental and advanced JavaScript concepts through questions and code samples from previous years' question papers.

Uploaded by

B Anushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views

Javascript Borad Paper Questions

The document provides sample questions from past SQP papers on JavaScript concepts and coding questions. It includes questions on JavaScript functions, operators, data types, arrays, strings and objects. Code snippets are provided to demonstrate concepts like parsing numbers to different bases, reversing arrays, extracting substrings and checking for occurrences of a word in a string. Predicted outputs and explanations are given for code examples. Overall, the document covers fundamental and advanced JavaScript concepts through questions and code samples from previous years' question papers.

Uploaded by

B Anushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SQP 20-21

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>

Predict output of the following : (2+2)


a) <html><body><script >
n =20;
m = n>>2;
x=n<<2;
document.write("value after right shift is : ", m, );
document.write("<br> value after left shift is : ", x);
</script></body></html>
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 :
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>

SQP Session 2019–2020


2M
Write code snippet in Java Script to find and display highest value from given four numbers num1,
num2, num3 and num4.
Ans :
x= Math.max(num1,num2, num3, num4);
document.write(“ Highest value is :” + x);
( 1 mark for finding the highest value
1 mark for displaying the highest value )
(2M)
Which of the following are valid Javascript variable names?
Add, function, average1, my.name, NaN, 2ndobject, obj_hello, wellness
Ans : Add, average1, obj_hello, wellness
(1/2 mark for each correct variable name)

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

Explain anonymous functions in JavaScript with an example. 3M


Ans: Anonymous functions are the functions without any name.
For example :
var avg = function() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {

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>

Give the output of the following code snippet with justification :


i) NaN % 5
ii) isFinite( -Infinity)
iii) “Early morning good morning”.replace(“morning”, “evening”)
iv) for (i=1;i<1;i++)
document.write(“hello”);
document.write(“ByeBye”);
v) str=” Health is Wealth”;
document.write(str.match(“wealth”);
document.write(str.match(“Wealth”);
Ans :
i) NaN
ii) false
iii) Early evening good morning
iv) ByeBye
v) null
Wealth
( 1 mark for each correct output)

SQP Session 2018-19


2M
Explain typeof operator in JavaScript with example.
Ans : The typeof is a unary operator that is placed before its single operand, which can be of
any type. Its value is a string indicating the data type of the operand.
eg: type of(5) will return number.

Explain substring( ) and reverse ( ) function with 2 examples

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>

Write code snippet in JavaScript for the following:


a) To display current date and time in the browser window.
Ans : var d=new Date();
document.write(d);
1 mark
b) To display first four characters of the string entered by the user in a variable
STR1
Ans :
function myFunction()
{
var str="hello! welcome to my world";
var n=str.substring(0,4);
document.getElementById("demo").innerHTML=n;
}
2 marks
c) To generate a random number and check whether it is even or odd.
Ans :
var m = math.random();
if (m%2 ==0)
document.write(“even”);
else
document.write(“odd”);
2 MARKS

2020 sample paper


2M
Give any two tips to remember while writing JavaScript commands. 2
29. Write the output of the given code : 2
<html>
<body>
<script>
s= ["blue", "red", "orange", "yellow"]
s.reverse();
for (i=0;i<s.length;i=i+2)
document.write(s[i].toUpperCase()+"<br>");
</script> </body> </html>
2020 new sqp
1)Name two functions associated with Stack in JavaScript. 1
2)Name the function used to find the length of a given string in JavaScript. 1
3)Ramesh has forgotten the data type of a variable in the program. Suggest appropriate command/function in
4)JavaScript to find the datatype of the variable. 1
Explain the use of type of attribute in <script> tag. 1
&& is a type of ___________ operator in JavaScript.

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.

2020 old sqp


2m
Differentiate between Property and Method with example.
Explain Fall through with respect to switch statement with the help of an
Example.

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>

Write statements in Javascript for the following : 5


(a) To store date and time in a variable and then display date and time on the webpage.
(b) (i) To store ‘‘Genius is one percent inspiration, 99% perspiration’’ in a variable.
(ii) To search ‘‘percent’’ in the string ‘‘Genius is one percent inspiration, 99% perspiration’’ and display it on the
webpage.

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.

Write any five features of JavaScript.

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.

Identify JavaScript methods for the following : 5


(a) To generate random numbers
(b) To return the nearest integer (For example : 78.9 to 80)
(c) To return the number with the highest value
(d) To find the position of the first occurrence of a text in a string
(e) To find the length of a string
2018

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.

Differentiate between if..else and switch statements in JavaScript.


(c) Consider the following and answer the questions that follow :
<EMBED SRC=‘‘work.mid’’ AUTOSTART=FALSE LOOP=TRUE WIDTH=145 HEIGHT=55 ALIGN=‘‘CENTER’’>
</EMBED>
(i) What is the tag <EMBED> used for ?
(ii) What is SRC used for ?
(iii) What will happen if AUTOSTART is set to TRUE ?
(iv) What will happen if LOOP is set to FLASE ?
(v) What will happen if HEIGHT and WIDTH are set to Zero (0) ?

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

Write a program in JavaScript to trigger a function ‘‘assign()’’ to set the


‘‘academic category’’ of students on the click of a button. The function
should accept the class of the student and then display the category using
confirm box. Category is as follows : 5
Classes : 1-5 ‘‘Primary’’
Classes : 6-8 ‘‘Middle’’
Classes : 9-10 ‘‘Secondary’’
Classes : 11-12 ‘‘Senior’’
Any other value : ‘‘Invalid’’

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

What may be the possible output of the following snippets : 1+2+2


(a) Predict the output of the following :
<html>
<body>
<script>
var fruits=["Apple", "mango"];
fruits[50]= "Orange";
document.write(fruits.length);
document.write("<br> end of program");
</script> </body> </html>

(b) Rewrite the following using switch case statement :


if(a==1)
document.write("Monday, Wednesday, Friday");
else if(a==2)
document.write("Tuesday, Thursday, Saturday");
else
document.write("Sunday");

You might also like