125). 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
Show Answer
Correct Answer: 105 as an integer value
Notes:
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.
126). Do functions in JavaScript necessarily return a value?
var grand_Total=eval("10*10+5");
[A]It is mandatory
[B]Not necessary
[C] Few functions return values by default
[D]some functions do not return any value
Show Answer
Correct Answer: Few functions return values by default
Notes:
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.
127). 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
Show Answer
Correct Answer: Yes, perfectly
Notes:
Answer: a
Explanation: Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.
128). What will be the output of the following JavaScript code?
var string2Num=parseInt("123xyz");
[A]123
[B]123xyz
[C]Exception
[D]NaN
Show Answer
Correct Answer: 123
Notes:
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.
129). The one-liner code that concatenates all strings passed into a function is ____________
var string2Num=parseInt("123xyz");
[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); }
Show Answer
Correct Answer: function concatenate() { return String.prototype.concat.apply('', arguments); }
Notes:
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.
130). If you have a function f and an object o, you can define a method named m of o with ________
var string2Num=parseInt("123xyz");
[A]o.m=m.f;
[B]o.m=f;
[C] o=f.m;
[D] o=f;
Show Answer
Correct Answer: o.m=f;
Notes:
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;
131). 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();
Show Answer
Correct Answer: var o= new Object;
Notes:
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.
132). 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
Show Answer
Correct Answer: The first line results in a real boolean value whereas the second line merely checks for the existence of the objects
Notes:
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.
133). 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
Show Answer
Correct Answer: 1
Notes:
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.
134). 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]()
Show Answer
Correct Answer: 10
Notes:
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.
135). What will be the output of the following JavaScript code?
var arr = [7, 5, 9, 1];
var min = Math.min.apply(null, arr);
document.writeln(min);
Show Answer
Correct Answer: 1
Notes:
Answer: c
Explanation: The function apply() is used to call a function that contains “this” value and the argument contains elements of an array. Unlike call() method, it contains a single array of arguments.
136). What will be the output of the following JavaScript code?
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
Show Answer
Correct Answer: 7
Notes:
Answer: d
Explanation: add function is defined in the first line of the code using the new property. Add function returns the sum of the two arguments passed to it.
137). What will be the output of the following JavaScript code?
var a=3.7;
var b=2;
a=ciel(a)
document.writeIn(a*b);
Show Answer
Correct Answer: 8
Notes:
Answer: d
Explanation: The ciel function in javascript round offs the value passed in its argument to the next higher closest value. Therefore the value of a will be converted to 4 and hence the output will be 8.
138). What will be the output of the following JavaScript code?
var a=2.99;
var ans=floor(a)*floor(a)
console.log(ans);
[A]9
[B]8.31
[C]Error
[D]4
Show Answer
Correct Answer: 4
Notes:
Answer: d
Explanation: The floor method round offs the value of the decimal number to the lower limit. Therefore floor of a will be 2 which will result in the output of 4.
139). What will be the output of the following JavaScript code?
var a=225;
document.writeln(Math.sqrt(a));
[A]225
[B]15
[C]Error
[D]Undefined
Show Answer
Correct Answer: 15
Notes:
Answer: b
Explanation: The JavaScript math sqrt() method returns the square root of a number. If the provided number is negative, it returns NaN.
140). What will be the output of the following JavaScript code?
[A]0.80
[B]0.88
[C]0.50
[D]0.78
Show Answer
Correct Answer: 0.88
Notes:
Answer: b
Explanation: The asinh() method returns the hyperbolic arcsine of a number. The method is find in the math library of Javascript.
141). What will be the output of the following JavaScript code?
[A]1.00
[B]1.01
[C]1.05
[D]1.10
Show Answer
Correct Answer: 1.10
Notes:
Answer: d
Explanation: The atan2() method returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians. The number returned represents the counterclockwise angle in radians (not degrees) between the positive X axis and the point (x, y).