Array And Function - Study Mode
[#31] Consider the following code snippet : var c = counter () , d = counter ()
c . count () d . count () c . reset () c . count () d . count () The state stored in d is :
Correct Answer
(A) 1
Explanation
Solution: The state stored in d is 1 because d was not reset.
[#32] Consider the following code snippet : var c = counter () , d = counter ()
function constfuncs () { var funcs = []
for(var i = 0
i < 10
i ++ ) funcs [ i ] = function () { return i
}
return funcs
} var funcs = constfuncs ()
funcs [ 5 ]() What does the last statement return ?
Correct Answer
(C) 10
Explanation
Solution: The code above creates 10 closures, and stores them in an array. The closures are all defined within the same invocation of the function, so they share access to the variable i. When constfuncs() returns, the value of the variable i is 10, and all 10 closures share this value. Therefore, all the functions in the returned array of functions return the same value.
[#33] Consider the following code snippet : var scope = "global scope"
function checkscope () { var scope = "local scope"
function f ()
{
return scope
}
return f
What is the function of the above code snippet?
Correct Answer
(C) Returns the value in scope
Explanation
Solution: The above code snippet returns the value in scope.
[#34] Consider the code snippet given below var count = [ 1 ,, 3 ]
What is the observation made?
Correct Answer
(A) The omitted value takes “undefined”
Explanation
Solution: If you omit a value from an array literal, the omitted element is given the value.
[#35] Consider the following code snippet var a1 = [,,,]
var a2 = new Array ( 3 )
0 in a1 0 in a2 The result would be
Correct Answer
(A) true false
Explanation
Solution: a1 has an element with index 0 and a2 has no element with index 0.