Array And Function(Javascript ) Questions and Answers

Question 1. When does the function name become optional in JavaScript?
  1.    When the function is defined as a looping statement
  2.    When the function is defined as expressions
  3.    When the function is predefined
  4.    All of the mentioned
Explanation:-
Answer: Option B. -> When the function is defined as expressions
The function name is optional for functions defined as expressions. A function declaration statement actually declares a variable and assigns a function object to it.

Question 2. Consider the following code snippet
function printprops(o)
{
for(var p in o)
console.log(p + ": " + o[p] + "n");
}
What will the above code snippet result ?
  1.    Prints the contents of each property of o
  2.    Returns undefined
  3.    All of the mentioned
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> Returns undefined
The above code snippet returns undefined.

Question 3. The function definitions in JavaScript begins with
  1.    Identifier and Parantheses
  2.    Return type and Identifier
  3.    Return type, Function keyword, Identifier and Parantheses
  4.    Identifier and Return type
Explanation:-
Answer: Option A. -> Identifier and Parantheses
The function definitions begin with the keyword function followed by an identifier that names the function and a pair of parantheses around a comma-separated list of zero or more identifiers.

Question 4. What will happen if a return statement does not have an associated expression?
  1.    It returns the value 0
  2.    It will throw an exception
  3.    It returns the undefined value
  4.    None of the mentioned
Explanation:-
Answer: Option C. -> It returns the undefined value
If the return statement does not have an associated expression, it returns the undefined value.

Question 5. What is the purpose of a return statement in a function?
  1.    Returns the value and continues executing rest of the statements, if any
  2.    Returns the value and stops the program
  3.    Returns the value and stops executing the function
  4.    Stops executing the function and returns the value
Explanation:-
Answer: Option D. -> Stops executing the function and returns the value
The return statement causes the function to stop executing and to return the value of its expression (if any) to the caller.

Question 6. Do functions in JavaScript necessarily return a value ?
  1.    It is mandatory
  2.    Not necessary
  3.    Few functions return values by default
  4.    All of the mentioned
Explanation:-
Answer: Option C. -> Few functions return values by default

Question 7. Consider the following code snippet :
var string2Num=parseInt("123xyz");
The result for the above code snippet would be :
  1.    123
  2.    123xyz
  3.    Exception
  4.    NaN
Explanation:-
Answer: Option B. -> 123xyz
The parseInt() function returns the first integer contained in the string or 0 if the string does not begin with an integer.

Question 8. For the below mentioned code snippet:
var o = new Object();
The equivalent statement is:
  1.    var o = Object();
  2.    var o;
  3.    var o= new Object;
  4.    Object o=new Object();
Explanation:-
Answer: Option C. -> var o= new Object;
You can always omit a pair of empty parentheses in a constructor invocation.

Question 9. Consider the following code snippet :
var tensquared = (function(x) {return x*x;}(10));
Will the above code work ?
  1.    Yes, perfectly
  2.    Error
  3.    Exception will be thrown
  4.    Memory leak
Explanation:-
Answer: Option A. -> Yes, perfectly
Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.

Question 10. If you have a function f and an object o, you can define a method named m of o with
  1.    o.m=m.f;
  2.    o.m=f;
  3.    o=f.m;
  4.    o=f;
Explanation:-
Answer: Option A. -> o.m=m.f;
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;