JavaScript Questions & Answers – Defining and Invoking Functions

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Defining and Invoking Functions”.

1. The function definitions in JavaScript begins with _____________
a) Identifier and Parentheses
b) Return type and Identifier
c) Return type, Function keyword, Identifier and Parentheses
d) Identifier and Return type
View Answer

Answer: c
Explanation: The function definitions begin with the keyword function followed by an identifier that names the function and a pair of parentheses around a comma-separated list of zero or more identifiers.

2. What will be the output of the following JavaScript code?

function printprops(o) 
{
    for(var p in o)
      console.log(p + ": " + o[p] + "\n");
}

a) Prints the contents of each property of o
b) Returns undefined
c) Prints only one property
d) Prints the address of elements
View Answer

Answer: b
Explanation: The above code snippet returns undefined.
advertisement
advertisement

3. When does the function name become optional in JavaScript?
a) When the function is defined as a looping statement
b) When the function is defined as expressions
c) When the function is predefined
d) when the function is called
View Answer

Answer: b
Explanation: 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.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. What is the purpose of a return statement in a function?
a) Returns the value and continues executing rest of the statements, if any
b) Returns the value and stops the program
c) Returns the value and stops executing the function
d) Stops executing the function and returns the value
View Answer

Answer: d
Explanation: The return stops the execution of the function when it is encountered within the function. It returns the value to the statement where the function is called.

5. What will happen if a return statement does not have an associated expression?
a) It returns the value 0
b) It will throw an exception
c) It returns the undefined value
d) It will throw an error
View Answer

Answer: c
Explanation: A function without a return statement will return a default value. If the return statement does not have an associated expression then it returns an undefined value.
advertisement

6. A function with no return value is called ___________
a) Procedures
b) Method
c) Static function
d) Dynamic function
View Answer

Answer: a
Explanation: Void functions does not return a value. Functions with no return value are sometimes called procedures.

7. The function stops its execution when it encounters?
a) continue statement
b) break statement
c) goto statement
d) return statement
View Answer

Answer: d
Explanation: Continue statement and break statement are used in the loops for skipping the iteration or going out of the loop. Whenever a return statement is encountered the function execution is stopped.
advertisement

8. Which keyword is used to define the function in javascript?
a) void
b) int
c) function
d) main
View Answer

Answer: c
Explanation: A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses(). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

9. Which is an equivalent code to invoke a function m of class o that expects two arguments x and y?
a) o(x,y);
b) o.m(x) && o.m(y);
c) m(x,y);
d) o.m(x,y);
View Answer

Answer: d
Explanation: The two argument in a function are separated by a comma (,). The code above is an invocation expression: it includes a function expression o.m and two argument expressions, x and y.

10. What will be the equivalent code of the following JavaScript code?

o.m(x,y);

a) o.m(x) && o.m(y);
b) o[“m”](x,y);
c) o(m)[“x”,”y”];
d) o.m(x && y);
View Answer

Answer: b
Explanation: Another way to write o.m(x,y) is o[“m”](x,y).o[“m”] will access the property of the object and parenthesis will access the function present inside it.

11. What will be the output of the following JavaScript code?

function info()
{  
    let a=1;
    let b=2;
    return a*b;  
}  
document.write(info());

a) 1
b) 2
c) 3
d) error
View Answer

Answer: b
Explanation: document.write() is used to write the output on the console. In the above code document.write() passes a function as its argument and the function returns 2 which is printed as output.

12. What will be the output of the following JavaScript code?

var arr = [7, 5, 9, 1];  
var value = Math.max.apply(null, arr);  
document.writeln(value);

a) 7
b) 5
c) 1
d) 9
View Answer

Answer: d
Explanation: In JavaScript, the first argument of apply() is used to specify the value of this within the function that apply() is calling. In the case of Math.max.apply(), the Math.max() function doesn’t rely on the value of this within its execution. It simply calculates the maximum value among the provided numbers. Therefore, the first argument is not crucial in this specific case and it’s often set to null.

13. What will be the output of the following JavaScript code?

var person = {  
      name: “Rahul”,  
      getName: function() 
      {  
          return this.name;  
      }  
} 
var unboundName = person.getName;  
var boundName = unboundName.bind(person);  
document.writeln(boundName());

a) runtime error
b) compilation error
c) Rahul
d) undefined
View Answer

Answer: c
Explanation: The javascript bind() function is used to create a new function. The newly created function has its own set of keywords.

14. What will be the output of the following JavaScript code?

function code(id,name) 
{  
  		this.id = id;   
  		this.name = name;  
}  
function pcode(id,name) 
{  
 		code.call(this,id,name);  
}
document.writeln(new pcode(101,"vivek").id);

a) vivek
b) 101
c) Runtime error
d) Compilation error
View Answer

Answer: b
Explanation: The JavaScript call() method is used to call a function containing “this” value as an argument. It returns the result of calling function.

15. What will be the output of the following JavaScript code?

     var pow=new Function("num1","num2","return Math.pow(num1,num2)");  
     document.writeln(pow(2,3));

a) 2
b) 3
c) 8
d) error
View Answer

Answer: c
Explanation: pow function is a predefined function which is present in the math library of javascript. The pow function accepts two arguments of which power of the first argument is calculated with respect to the second.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.