HSLC Computer Science Solution: Chapter 6 (Introduction to JavaScript) - UJUDEBUG

HSLC Computer Science Solution: Chapter 6 (Introduction to JavaScript)

Empty Blackboard

Contents:

 

 

  • Objective Type Questions:

1. Fill in the blanks:

a. The …………………… box is used to display information to the user.
ans: message

b. A …………………… is a storage area where value is stored and retrieved when required.
ans: variable

c. The …………………… operator finds out the remainder.
ans: modulus

d. The …………………… operator is a unary operator.
ans: !NOT

e. The …………………… operator uses ? (question mark) and : (colon) characters.
ans: conditional

2. True and False

a. The semicolon at the end of simple statements in JavaScript is mandatory.
ans: True

b. In JavaScript MARKS and marks are treated as the same variable.
ans: False

c. A simple statement must be enclosed in braces.
ans: False

d. A compound statement consists of several instructions grouped in a block.
ans: True

e. The switch statement can only be used to compare an expression against constants.
ans: True

3. Choose the correct option.

a. Which one is an invalid variable name in JavaScript?
i.1employeename
ii. Employeename
iii. Employee_Name
iv. EmployeeName

ans: i.1employeename

b. A block of statements is enclosed within
i. ( )
ii.{ }
iii.[]
iv. ” ”

ans: ii.{ }

c. The statement used to exit from the switch…case statement is
i. goto
ii. break
iii. continue
iv. exit

ans: ii. break

d. …………………… takes a number as input from the user.
i. Alert message
ii.prompt dialog box
iii. Confirm dialog box
iv. All of these

ans: ii.prompt dialog box

e. Which one of these is not an arithmetic operator in JavaScript?
i. *
ii.^
iii.+
iv. /

ans: ii.^

f. == is a/an
i. Arithmetic operator
ii. relational operator
iii. Compound Assignment operator
iv. Logical operator

ans: ii. relational operator

  • Descriptive Type Questions

1. Short Answer Questions

a. What are the three data types in JavaScript?
ans: The three types of data types in JavaScript are:
(a) Numbers Eg. 453, 98.12, etc.
(b) Strings Eg. “Python Programming”, “grade”, etc.
(c) Boolean Eg. True or False

b. How will you declare variables subl, sub2, and sub3 in JavaScript?
ans: In JavaScript, when we are declaring a variable, we start with the letters ‘var’. After a space, we then need to write the variable name.
Therefore, we will declare variables sub1, sub2, and sub3 as:
var sub1, sub2, sub3

c. Name the different forms of if statement.
ans: if statement has the following formats – if, if…..else and if….else if.

d. Name the three logical operators.
ans: JavaScript supports three logical operators. &&(AND), || (OR) and !!(NOT)

e. Write the statements equivalent to A++ and A— —.
ans: The statements equivalent to A++ is increment operator and A– is decrement operator.

f. Write the shorthand form for the following:
A = A + 15
B = B +=1

ans: A=A+15
B=B+=15

2. Long answer questions

a. Explain the two types of comments in JavaScript.
ans: • Single line comment It starts with a pair of slash sign (//). The browser ignores anything from the pair of slash sign (//) till the end of that same line. For example:
// Single line comment

• Multi-line comment This is also known as block comment. It includes more than one line between /* and */ characters: The browser ignores the statements between these characters. For example:
/* This is a block comment */

b. What are the rules for naming a JavaScript variable?
ans: Rules for. Naming a Variable are
• The name must begin with a letter or the underscore character. It should not start with a digit. The first character is followed by letters, digits, or underscore sign.
• It should not be a reserved word.
• We cannot have spaces in a variable name.
• JavaScript is case sensitive. Thus, for example, name, NAME, and Name are treated as three different variables.

c. What are the limitations of switch statement?
ans: The switch statement can only be used to compare an expression against constants. Therefore,
• The case label cannot be a variable. For example, case n: where n is a variable.
• The case label cannot be a range. For example, case (1..3):. This is not a valid constant.

d. What is the difference between simple and compound statements?
ans:

Simple Statements

Compound Statements

1.

A simple statement consists of a single instruction.

A compound statement consists of several instructions grouped in a block

2.

It doesn’t require braces to enclose it.

Such statements must be enclosed in braces if we wish to treat them all as a single statement.

e. Explain the working of conditional operator with an example.
ans: The conditional operator evaluates an expression and returns the value written after ‘?’ if the condition evaluated is true. In case the result is false, the value after ‘:’ is returned. Its general format is:
var result = condition ? “TRUE”.: “FALSE”;

To understand the concept of this operator, we do the following:

1. Type the following JavaScript code in Notepad and save it as .html file.
<!DOCTYPE html>
<html>
<body>
<script language=”javascript” type=”text/javascript”>
var age, result
age = 20,a,
result = age > 18 ? “You are over 18 years” : “You are below or equal to 18 years”
document.write(result)

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

2. Open the HTML file in Internet Explorer. The output will appear as ‘You are over 18 years’.

 

  • Application-based Questions

1. Jasmine has written the given code to display a message box on the screen, but the message box is not showing on the screen. Find the problem in this code and rewrite it to display the given statement in a message box.

<!DOCTYPE html>
<html>
<body>
alert (“This is my first javaScript code”) </body>
</html>

ans: The correct code for the above question is:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
alert (“This is my first javaScript code”)
</script>
</body>
</html>

2.Rewrite the following code using if…else if statement.

switch (direction)
{
case “N” : a = “North”; break;
case “E” : a = “East”; break;
case “W” : a = “West”; break;
case “S” : a = “South”; break;
default : a = “Enter correct code”; 1
}
document .write (a)

ans:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
x = prompt(“Enter Code- N/E/W/S”)

if (x==”N”) {
document.write (“North”)
}
else if (x==”E”) {
document.write (“East”)
}
else if (x==”W”) {
document.write (“West”)
}
else if (x==”S”) {
document.write (“South”)
}
else{
document.write (“Enter correct Code”)
}
</script>
</body>
</html>

3. What will be the output of the following code:

var a, b
a = 5
b = 10
b += a
a++
b–
document.write (“a = ” + a + “<br>”)
document.write (“b = ” + b)

ans: a=16
b=14

4. Evaluate the following arithmetic expression and find out its output, assuming b = 4 and c = 4.
a=b*3/ 4+c/ 4+8—b +5/8

ans: Code:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var a, b, c
b=4
c=4
a=b*3/4+c/4+8-b+5/8
document.write(“a =”+ a + “<br>”)
</script>
</body>
</html>

Output: a=8.625

5. Assuming A=5 and B=6, what will be the result of the following statements:
i. ((A != B) && (A >= B))
ii. ((A != B) || (A <= B))
iii. !(A > B)
ans:
i. Code:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var A,B,C
A=5
B=6
C=((A != B) && (A >= B))
document.write(“Output = “+ C + “<br>”)
</script>
</body>
</html>

Output: false

ii. Code:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var A,B,C
A=5
B=6
C=((A != B) || (A <= B))
document.write(“Output = “+ C + “<br>”)
</script>
</body>
</html>

Output: true

iii. Code:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var A,B,C
A=5
B=6
C=!(A > B)
document.write(“Output = “+ C + “<br>”)
</script>
</body>
</html>

Output: true

6. Write the following if…else statement using the conditional operator.
if (x == 100)
a = “x is 100”
else
a = “x is not 100”

ans:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var x,result
x = prompt(“Enter Value”)
result = x==100 ? “x is 100” : “x is not 100”
document.write(result)
</script>
</body>
</html>
Interested? Have any questions?

support@ujudebug.com

For more info Call us

+91 8486 201473

2 Comments

  1. […] Chapter 6: Introduction to JavaScript […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Call Us Now:

Sales : +91-8011624355
Support : +91-8486201473
Support : +91-7002890335

Lowest Price Promise

[one_third] [/one_third] [two_third] We’re all looking for the best prices, even saving a couple of rupees can make a huge difference. That’s why we’ll match the price of your products if you find them cheaper somewhere else.

Our price match promise

We'll price match any item we sell against any other seller. Here’s what you need to do to you get the best price:
  • Give us a call on +918486201473
  • Let our friendly team know the product
  • Tell us where you spotted it
  • Get it on cheapest price
That’s it! The whole thing should only take a few minutes and you’ll save some money too. Winner winner! [/two_third]

Lowest Price Promise

We’re all looking for the best prices, even saving a couple of rupees can make a huge difference. That’s why we’ll match the price of your products if you find them cheaper somewhere else.

Our price match promise

We'll price match any item we sell against any other seller. Here’s what you need to do to you get the best price:

  • Give us a call on +918486201473
  • Let our friendly team know the product
  • Tell us where you spotted it
  • Get it on cheapest price

That’s it! The whole thing should only take a few minutes and you’ll save some money too. Winner winner!