JavaScript Questions and Answers Part-17

1. What will be the output of the following JavaScript code?
let x=x+1;
console.log(x);
a) 0
b) null
c) Reference error
d) NaN

Answer: d
Explanation: x has not been defined with any value hence the value of x+1 will be Nan. Therefore the console.log function will print NaN.

2. What will be the output of the following JavaScript code?
[x,y]=[y,x];
a) Throws exception
b) Swap the value of the two variables
c) Flashes an error
d) Creates a new reference object

Answer: c
Explanation: The above code snippet will flash an error message. This is due to the fact that the variables x and y are not defined.

3. Which looping statement allows XML tags to appear in JavaScript programs and adds API for operating on XML data?
a) for loop
b) while loop
c) for/each loop
d) do while loop

Answer: c
Explanation: The for/each loop is a new looping statement standardized by E4X. E4X (ECMAScript for XML) is a language extension that allows XML tags to appear literally in JavaScript programs and adds syntax and API for operating on XML data.

4. Which exception does the Iterators throw from their next() method when there are no more values to iterate, that work on finite collections?
a) Exit Iteration
b) Abort Iteration
c) Abort
d) Stop Iteration

Answer: d
Explanation: Iterators that work on finite collections throw Stop Iteration from their next() method when there are no more values to iterate. Stop Iteration is a property of the global object in JavaScript 1.7. Its value is an ordinary object (with no properties of its own) that is reserved for this special purpose of terminating iterations. Note, in particular,that Stop Iteration is not a constructor function like TypeError() or RangeError().

5. Which method of the iterable object returns an iterator object for the collection?
a) iterator()
b) _iterator_()
c) _iteration_()
d) _return_iterator_()

Answer: b
Explanation: An iterable object represents a collection of values that can be iterated. An iterable object must define a method named __iterator__() (with two underscores at the start and end of the name) which returns an iterator object for the collection.

6. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var res = "";
res=res + Number.isSafeInteger(Math.pow(2, 53)-1)+": 253-1
";
document.getElementById("demo").innerHTML = res;
}
</script>
a) True
b) false
c) error
d) undefined

Answer: a
Explanation: The Number.isSafeInteger() method determines whether a value is a safe integer. A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number (all integers from (253 – 1) to -(253 – 1)).

7. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML = Number.MAX_VALUE;
}
</script>
a) 1.7976931348623157e+308
b) 1.7976931348623157e+305
c) 1.7976931348623157e+307
d) error

Answer: a
Explanation: The MAX_VALUE property returns the largest number possible in JavaScript. This static property has a value of 1.7976931348623157e+308.

8. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML = Number.NEGATIVE_INFINITY;
}
</script>
a) -1000
b) -infinity
c) infinity
d) undefined

Answer: b
Explanation: The NEGATIVE_INFINITY property represents negative infinity. Negative infinity can be explained as something that is lower than any other number.

9. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var x = 100;
document.getElementById("demo").innerHTML = x.NEGATIVE_INFINITY;
}
</script>
a) true
b) false
c) error
d) undefined

Answer: d
Explanation: NEGATIVE_INFINITY is a static property of the JavaScript Number object. Using x.NEGATIVE_INFINITY, where x is a number or a Number object, will return undefined.

10. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
function myFunction()
{
var num = 5.56789;
var n = num.toExponential();
document.getElementById("demo").innerHTML = n;
}
</script>
a) 5.56789e+0
b) 5.57e+0
c) 5.568e+0
d) Error

Answer: a
Explanation: The toExponential() method converts a number into an exponential notation. It displays the number of digit according to the input passed to it.