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?

[email protected]

For more info Call us

+918011624355

5 Comments

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

  2. Himakshi talukdar says:

    It helped me a lot tq …. But if I could download it as pdf it would be better … tq

  3. S c says:

    Pdf file is needed

Leave a Reply

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

Call us now!

[one_third] [/one_third] [two_third] With a team of skilled software Developers, Ujudebug is the best IT Solution Company in Assam. Fully Customized Software Development services with multiple platforms. Our award winning team will we provide the best experience to customers at an affordable price.

Sales : +91-8011624355

Support : +91-8486201473

Our promise

We promise to provide exceptional service during your journey with us. Our experienced team delivers an unmatched level of client satisfaction from a broad range of software solutions, and IT consulting services. What are you waiting for:
  • - Give us a call on +918011624355
  • - Let our friendly team know your project
  • - Tell us how, when you want to start it
  • - Get it at the best possible price with all documentation
That’s it! The whole thing should only take a few minutes. [/two_third]

Call us now!

With a team of skilled software Developers, Ujudebug is the best IT Solution Company in Assam. Fully Customized Software Development services with multiple platforms. Our award winning team will we provide the best experience to customers at an affordable price.

Sales : +91-8011624355

Support : +91-8486201473

Our promise

We promise to provide exceptional service during your journey with us. Our experienced team delivers an unmatched level of client satisfaction from a broad range of software solutions, and IT consulting services. What are you waiting for:

  • - Give us a call on +918011624355
  • - Let our friendly team know your project
  • - Tell us how, when you want to start it
  • - Get it at the best possible price with all documentation

That’s it! The whole thing should only take a few minutes.