JavaScript Questions and Answers Part-2

1. JavaScript _________ when there is an indefinite or an infinite value during an arithmetic computation.
a) Prints an overflow error
b) Prints an exception error
c) Displays “Infinity”
d) Prints the value as such

Answer: c
Explanation: When the result of a numeric operation is larger than the largest representable number (overflow), JavaScript prints the value as Infinity.

2. The statement a===b refers to _________
a) Both a and b are equal in value and type
b) Both a and b are equal in value, type and reference address
c) Both a and b are equal in value
d) There is no such statement

Answer: a
Explanation: ”===” operator is known as the strict comparison operator. A strict comparison (===) is only true if the operands are of the same type and the contents match.

3. What will be the output of the following JavaScript code?
function equalto()
{
int num=10;
if(num===”10”)
return true;
else
return false;
}
a) false
b) true
c) compilation error
d) runtime error

Answer: b
Explanation: A === operator is only true if the operands are of the same type and the contents match. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.

4. What will be the output of the following JavaScript code?
int a==2;
int b=4;
int ans=a+b;
print(ans);
a) 0
b) 2
c) 6
d) error

Answer: d
Explanation: The following code will generate into an error output as a comparator operator is used outside of if statement. A single equalto (’=’) operator is used for initialization.

5. The escape sequence ‘\f’ stands for _________
a) \f is not present in JavaScript
b) Floating numbers
c) Representation of functions that returns a value
d) Form feed

Answer: d
Explanation: \f is the JavaScript escape sequence that stands for Form feed (\u000C). It skips to the start of the next page.

6. Which of the following Attribute is used to include External JS code inside your HTML Document?
a) ext
b) src
c) link
d) script

Answer: b
Explanation: Script “tag” is used to include the JavaScript code. To include external JavaScript files “src” attribute is used inside the script tag.

7. The type of a variable that is volatile is _______________
a) Immutable variable
b) Mutable variable
c) Dynamic variable
d) Volatile variable

Answer: b
Explanation: The variables whose values can be changed are called mutable variable types. In JavaScript, only objects and arrays are mutable, not primitive values.

8. What will be the output of the following JavaScript code?
int a=1;
if(a!=null)
return 1;
else
return 0;
a) runtime error
b) compiler error
c) 1
d) 0

Answer: c
Explanation: != is the not equal to operator. It gives a value of 1 if the two values which are compared are not equal and give 0 if the two values are equal.

9. The snippet that has to be used to check if “a” is not equal to “null” is _________
a) if(a!=null)
b) if(a!null)
c) if (!a)
d) if(a!==null)

Answer: d
Explanation: A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison. The not-equal operator !== compares 0 to null and evaluates to either true or false.

10. What will be the output of the following JavaScript code?
function compare()
{
int a=1;
char b=1;
if(a.tostring()===b)
return true;
else
return false;
}
a) runtime error
b) logical error
c) true
d) false

Answer: c
Explanation: A non string (integer) can be converted to string using .tostring() function. A strict comparison is only true if the operands are of the same type and the contents match. Hence the following code snippet would result into true output.