JavaScript Questions and Answers Part-9

1. What will be the output of the following JavaScript statement?
var grand_Total=eval("10*10+5");
a) 10*10+5
b) 105 as a string
c) 105 as an integer value
d) Exception is thrown

Answer: c
Explanation: eval() is a function property of the global object. The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements.

2. Do functions in JavaScript necessarily return a value?
a) It is mandatory
b) Not necessary
c) Few functions return values by default
d) some functions do not return any value

Answer: c
Explanation: Functions generally have a return statement and hence usually returns a value. Some functions which does not have a return statement returns value by default during execution.

3. Will the following JavaScript code work?
var tensquared = (function(x) {return x*x;}(10));
a) Yes, perfectly
b) error
c) Exception will be thrown
d) Memory leak

Answer: a
Explanation: Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.

4. What will be the output of the following JavaScript code?
var string2Num=parseInt("123xyz");
a) 123
b) 123xyz
c) Exception
d) NaN

Answer: a
Explanation: The parseInt() function parses a string and returns an integer. The function returns the first integer contained in the string or 0 if the string does not begin with an integer.

5. The one-liner code that concatenates all strings passed into a function is ____________
a) function concatenate()
{
return String.prototype.concat('', arguments);
}
b) function concatenate()
{
return String.prototype.apply('', arguments);
}
c) function concatenate()
{
return String.concat.apply('', arguments);
}
d) function concatenate()
{
return String.prototype.concat.apply('', arguments);
}

Answer: d
Explanation: The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. apply takes an array of arguments and treats each element of that array as a single argument.

6. If you have a function f and an object o, you can define a method named m of o with ________
a) o.m=m.f;
b) o.m=f;
c) o=f.m;
d) o=f;

Answer: b
Explanation: 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;

7. What will be the equivalent statement of the following JavaScript statement?
var o = new Object();
a) var o = Object();
b) var o;
c) var o= new Object;
d) Object o=new Object();

Answer: c
Explanation: As a special case, for the new operator only, JavaScript simplifies the grammar by allowing the parenthesis to be omitted if there are no arguments in the function call. Hence you can always omit a pair of empty parentheses in a constructor invocation.

8. What is the difference between the two lines in the following JavaScript code?
!!(obj1 && obj2);
(obj1 && obj2);
a) Both the lines result in a boolean value “True”
b) Both the lines result in a boolean value “False”
c) Both the lines check just for the existence of the object alone
d) The first line results in a real boolean value whereas the second line merely checks for the existence of the objects

Answer: d
Explanation: The first returns a “real” boolean value, because you first negate what is inside the parenthesis, but then immediately negate it again. So, it’s like saying something is “not not” truth-y, making it true. The second example simply checks for the existence of the obj1 and obj2, but might not necessarily return a “real” boolean value, instead returning something that is either truth-y or false-y. This can be problematic, because false-y can be the number 0, or an empty string, etc. Simple existence can be truth-y. A “real” boolean will only be true or false.

9. What will be the state stored in d in the following JavaScript code?
var c = counter(), d = counter();
c.count()
d.count()
c.reset()
c.count()
d.count()
a) 1
b) 0
c) null
d) Undefined

Answer: a
Explanation: Counter function increments the value of the variable by 1 and reset function sets the value of the variable back to 0.as d is incremented twice and reset function is not called on d therefore the value of d is 2.

10. What will be the last statement return in the following JavaScript code?
function constfuncs()
{
var funcs = [];
for(var i = 0; i < 10; i++)
funcs[i] = function() { return i; };
return funcs;
}
var funcs = constfuncs();
funcs[5]()
a) 9
b) 0
c) 10
d) 12

Answer: c
Explanation: 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.