This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Functions and Functional Programming”.
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
View Answer
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
View Answer
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
View Answer
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
View Answer
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); }
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;
View Answer
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();
View Answer
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
View Answer
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
View Answer
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
View Answer
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.
11. 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);
a) 7
b) 5
c) 1
d) 9
View Answer
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.
12. What will be the output of the following JavaScript code?
var add=new Function("num1","num2","return num1+num2"); document.writeln(add(2,5));
a) 2
b) 5
c) Error
d) 7
View Answer
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.
13. What will be the output of the following JavaScript code?
var a=3.7; var b=2; a=ciel(a) document.writeIn(a*b);
a) 6
b) 7.4
c) 7.5
d) 8
View Answer
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.
14. 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
View Answer
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.
15. 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
View Answer
Explanation: The JavaScript math sqrt() method returns the square root of a number. If the provided number is negative, it returns NaN.
16. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.asinh(1); } </script>
a) 0.80
b) 0.88
c) 0.50
d) 0.78
View Answer
Explanation: The asinh() method returns the hyperbolic arcsine of a number. The method is find in the math library of Javascript.
17. What will be the output of the following JavaScript code?
<p id="demo"></p> <script> function myFunction() { document.getElementById("demo").innerHTML = Math.atan2(8, 4); } </script>
a) 1.00
b) 1.01
c) 1.05
d) 1.10
View Answer
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).
Sanfoundry Global Education & Learning Series – Javascript Programming.
- Apply for Computer Science Internship
- Check Programming Books
- Practice MCA MCQs
- Practice Information Science MCQs
- Check JavaScript Books