This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “JavaScript Extensions”.
1. What will be the output of the following JavaScript code?
const pi=3.14; var pi=4; console.log(pi);
a) This will flash an error
b) Prints 4
c) Prints 3.14
d) Ambiguity
View Answer
Explanation: Const keyword fixes the value of the variable. Const keyword can not be redefined. Therefore attempts to alter the value or re-declaration causes errors.
2. The let keyword cannot be used ___________
a) as a substitute of var
b) as a block statement to define new variables
c) to define variables that are scoped to a single expression
d) in a else if loop, as a substitute for var
View Answer
Explanation: The let keyword can be used in four ways :
- as a variable declaration like var;
- in a for or for/in loop, as a substitute for var;
- as a block statement, to define new variables and explicitly delimit their scope; and
- to define variables that are scoped to a single expression.
3. The main difference between the variables declared with var and with let is __________
a) var is confined to a particular function but let is not
b) let is confined to a particular function but var is not
c) var defines values based on conditions but let does not
d) let doesn’t let you change the value of the variable
View Answer
Explanation: Variables declared with var have global scope whereas variable declared with let have block scope. Variables declared with let are defined only within the closest enclosing block (and any blocks nested within it, of course).
4. What will be the output of the following JavaScript code if oddsums(5); is executed after the following code snippet?
function oddsums(n) { let total = 0, result=[]; for(let x = 1; x <= n; x++) { let odd = 2*x-1; total += odd; result.push(total); } return result; }
a) Returns [1,4,9,16,25]
b) Returns [1,2,3,4,5]
c) Returns [3,6,9,12,15]
d) Returns [1,3,5,7,9]
View Answer
Explanation: Let keyword has block scope thus in the above code snippet the total variable will be defined inside the for loop. The above code returns 1, 4, 9, 16, 25 which is the square of the first five natural numbers.
5. What would be the result or type of error if p is not defined in the following JavaScript code snippet?
console.log(p)
a) Zero
b) Null
c) Reference Error
d) Value not found Error
View Answer
Explanation: Console.log() is a predefined function in javascript which is used to print data or message on the console. If the argument of the console.log is not defined it will give a reference error.
6. 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
View Answer
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.
7. 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
View Answer
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.
8. 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
View Answer
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.
9. 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
View Answer
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().
10. Which method of the iterable object returns an iterator object for the collection?
a) iterator()
b) _iterator_()
c) _iteration_()
d) _return_iterator_()
View Answer
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.
11. 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)+": 2<sup>53</sup>-1<br>"; document.getElementById("demo").innerHTML = res; } </script>
a) True
b) False
c) Error
d) Undefined
View Answer
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)).
12. 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
View Answer
Explanation: The MAX_VALUE property returns the largest number possible in JavaScript. This static property has a value of 1.7976931348623157e+308.
13. 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
View Answer
Explanation: The NEGATIVE_INFINITY property represents negative infinity. Negative infinity can be explained as something that is lower than any other number.
14. 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) Undefiend
View Answer
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.
15. 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
View Answer
Explanation: The toExponential() method converts a number into an exponential notation. It displays the number of digit according to the input passed to it.
Sanfoundry Global Education & Learning Series – Javascript Programming.
- Check Programming Books
- Practice Programming MCQs
- Check JavaScript Books
- Practice Information Science MCQs
- Practice MCA MCQs