JavaScript Questions and Answers – Types, Values and Variables

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Types, Values and Variables”.

1. JavaScript Code can be called by using ___________
a) RMI
b) Triggering Event
c) Preprocessor
d) Function/Method
View Answer

Answer: d
Explanation: JavaScript code can be called by making a function call to the element on which JavaScript has to be run. There are many other methods like onclick, onload, and onsubmit etc.

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

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.

3. A hexadecimal literal begins with __________
a) 00
b) 0x
c) 0X
d) Both 0x and 0X
View Answer

Answer: d
Explanation: Generally, X or x denotes hexadecimal values. So, any integer literal that begins with 0X or 0x denotes a hexadecimal number.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

4. The generalised syntax for a real number representation is __________
a) [digits][.digits][(E|e)[(+|-)]digits]
b) [digits][+digits][(E|e)[(+|-)]digits]
c) [digits][(E|e)[(+|-)]digits]
d) [.digits][digits][(E|e)[(+|-)]digits]
View Answer

Answer: a
Explanation: Floating-point literals may also be represented using exponential notation: a real number followed by the letter e (or E), followed by an optional plus or minus sign, followed by an integer exponent. This notation represents the real number multiplied by 10 to the power of the exponent.

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

Answer: c
Explanation: When the result of a numeric operation is larger than the largest representable number (overflow), JavaScript prints the value as Infinity. Similarly, when a negative value becomes larger than the largest representable negative number, the result is negative infinity. The infinite values behave as you would expect: adding, subtracting, multiplying, or dividing them by anything results in an infinite value (possibly with the sign reversed).

6. Which of the following is not considered as an error in JavaScript?
a) Syntax error
b) Missing of semicolons
c) Division by zero
d) Missing of Bracket
View Answer

Answer: c
Explanation: Division by zero is not an error in JavaScript: it simply returns infinity or negative infinity. There is one exception, however: zero divided by zero does not have a well defined value, and the result of this operation is the special not-a-number value, printed as NaN.

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

Answer: d
Explanation: \f is the JavaScript escape sequence that stands for Form feed (\u000C). It skips to the start of the next page. (Applies mostly to terminals where the output device is a printer rather than a VDU).
advertisement

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

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.

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

Answer: c
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.
advertisement

10. Assume that we have to convert “false” that is a non-string to string. The command that we use is (without invoking the “new” operator).
a) false.toString()
b) String(false)
c) String newvariable=”false”
d) Both false.toString() and String(false)
View Answer

Answer: d
Explanation: The three approaches for converting to string are: value.toString(),”” + value and String(value). A non-string can be converted in two ways without using a new operator false.toString () and String(false).

11. What will be the output of the following JavaScript code?

function compare()
{
    int num=2;
    char b=2;
    if(a==b)
        return true;
    else
        return false;
}

a) true
b) false
c) runtime error
d) compilation error
View Answer

Answer: a
Explanation: The == convert different type of operands to the same type before making the comparison. A strict comparison results into true value if the operands are of the same type and the contents match.

12. What will be the output of the following JavaScript code?

function equalto()
{
    let num=10;
    if(num===10)
        return true;
    else
        return false;
}

a) true
b) false
c) runtime error
d) compilation error
View Answer

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.

13. 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) true
b) false
c) runtime error
d) logical error
View Answer

Answer: a
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.

14. What will be the output of the following JavaScript code?

int a==2;
int b=4;
int ans=a+b;
print(ans);

a) 2
b) 6
c) 0
d) error
View Answer

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.

15. What will be the output of the following JavaScript code?

int a=1;
if(a!=null)
    return 1;
else
    return 0;

a) 1
b) 0
c) runtime error
d) compiler error
View Answer

Answer: a
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.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.