JavaScript Questions and Answers Part-13

1. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
var x = 123e5;
document.getElementById("demo").innerHTML = x ;
</script>
a) 0.0123
b) 12300
c) error
d) undefined

Answer: b
Explanation: Extra large or extra small numbers can be written with scientific (exponential) notation. The number followed by e tells about the number of digits in the number.

2. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var res = "";
res = res + Number.isFinite(5-2) ;
document.getElementById("demo").innerHTML = res;
}
</script>
a) 3
b) true
c) false
d) error

Answer: b
Explanation: The Number.isFinite() method determines whether a value is a finite number. This method returns true if the value is of the type Number, and equates to a finite number. Otherwise, it returns false.

3. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var res = "";
res = res + Number.isFinite(0 / 0);
document.getElementById("demo").innerHTML = res;
}
</script>
a) 3
b) true
c) false
d) error

Answer: c
Explanation: The Number.isFinite() method determines whether a value is a finite number. The above code results into an infinte number therefore the output will be false.

4. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var res = "";
res = res + Number.isInteger('123');
document.getElementById("demo").innerHTML = res;
}
</script>
a) true
b) false
c) error
d) Undefined

Answer: b
Explanation: The Number.isInteger() method determines whether a value an integer. This method returns true if the value is of the type Number.

5. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var res = "";
res = res + Number.isInteger(0.5) + ": 0.5
";
document.getElementById("demo").innerHTML = res;
}
</script>
a) true
b) false
c) error
d) undefined

Answer: b
Explanation: isInteger method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise it returns false.

6. The ‘$’ present in the RegExp object is called a ____________
a) character
b) matcher
c) metacharacter
d) metadata

Answer: c
Explanation: The ‘S’ is a special metacharacter that matches the end of a string. It is used to define or access elements in jquery.

7. Consider the following JavaScript statement containing regular expressions and check if the pattern matches?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
a) text==pattern
b) text.equals(pattern)
c) text.test(pattern)
d) pattern.test(text)

Answer: d
Explanation: The given pattern is applied to the text given in the parenthesis. The test() method tests for a match in a string. This method returns true if it finds a match, otherwise it returns false.

8. The regular expression to match any one character not between the brackets is _________
a) […]
b) [^]
c) [^…]
d) [\D]

Answer: c
Explanation: RegExp defines a special set of character that is used to do manipulation on strings and other variables. The [^…] character class is used to match or draw any one character not between the brackets.

9. What does /[^(]* regular expression indicate?
a) Match one or more characters that are not open parenthesis
b) Match zero or more characters that are open parenthesis
c) Match zero or more characters that are not open parenthesis
d) Match one or more characters that are open parenthesis

Answer: c
Explanation: The [^…] character class is used to match or draw any one character not between the brackets. One should always be careful while using * and ? as repetition characters as they may match zero instances of whatever precedes them, they are allowed to match nothing.

10. What will be the result when non greedy repetition is used on the pattern /a+?b/?
a) Matches the letter b preceded by the fewest number of a’s possible
b) Matches the letter b preceded by any number of a
c) Matches letter a preceded by letter b, in the stack order
d) Matches letter a present in the string

Answer: a
Explanation: Using non greedy repetition may not always produce the results you expect. /a+?b/ matches the letter b preceded by the fewest number of a’s possible.