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.
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).
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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()
{
    let a=2.0;
    let 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. In this case the value of b which is 2 gets converted to floating 2.0 and then the comparison is made. Hence, the result will be true.

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: The function equalto() compares the value of the variable num with the string “10” using the strict equality operator ===. The variable numis assigned the value 10, which is a number. The string “10” is compared to the number 10. Since === checks for both value and type equality without performing type coercion, the number 10 (which is of type number) is not strictly equal to the string “10” (which is of type string). So, the function will return false.

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

function compare()
{
    let a=1;
    let 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 given code snippet would result into true output.

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

let a = 2;
let b = 4;
let ans = a + b;
console.log(ans);

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

Answer: b
Explanation: The addition operation + is used to add the values of a and b. console.log() is used to print the result to the console. When you run this code in a JavaScript environment, it will output 6, which is the result of adding 2 and 4.

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

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

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

Answer: b
Explanation: In JavaScript, if a variable is declared but not initialized, it is automatically assigned the value undefined. In JavaScript, null and undefined are distinct values. So, the condition a !== null will evaluate to true, because a is not explicitly assigned the value null.

Sanfoundry Global Education & Learning Series – Javascript Programming.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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 & discussions at Telegram SanfoundryClasses.