ExamVeda
Login
Home
21
Do functions in JavaScript necessarily return a value ?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
22
Consider the following code snippet :
var tensquared = (function(x) {return x*x;}(10));
Will the above code work ?
Discuss
Answer & Solution
Answer: Option A
Solution:
Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.
23
Consider the following code snippet :
var string2Num=parseInt("123xyz");
The result for the above code snippet would be :
Discuss
Answer & Solution
Answer: Option B
Solution:
The parseInt() function returns the first integer contained in the string or 0 if the string does not begin with an integer.
24
If you have a function f and an object o, you can define a method named m of o with
Discuss
Answer & Solution
Answer: Option A
Solution:
A method is nothing more than a JavaScript function that is stored in a property of an object. If you have a function f and an object o, you can define a method named m of o with the following line:
o.m = f;
25
For the below mentioned code snippet:
var o = new Object();
The equivalent statement is:
Discuss
Answer & Solution
Answer: Option C
Solution:
You can always omit a pair of empty parentheses in a constructor invocation.
26
What is the difference between the two lines given below ?
!!(obj1 && obj2);
(obj1 && obj2);
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
27
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 :
Discuss
Answer & Solution
Answer: Option A
Solution:
The state stored in d is 1 because d was not reset.
28
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 ?
Discuss
Answer & Solution
Answer: Option C
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.
29
What kind of scoping does JavaScript use?
Discuss
Answer & Solution
Answer: Option B
Solution:
Like most modern programming languages, JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked.
30
What must be done in order to implement Lexical Scoping?
Discuss
Answer & Solution
Answer: Option C
Solution:
In order to implement lexical scoping, the internal state of a JavaScript function object must include not only the code of the function but also a reference to the current scope chain.