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.
advertisement

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.

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.

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.

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.
advertisement

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

function info()
{  
    int a=1;
    int 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: apply() is a predefined function method in javascript. The argument of apply() method contains elements of an array. It contains two values as argument which are optional as input.

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

var person = 
{  
      name: “Rahul”,  
      getName: function() 
      {  
          nreturn 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.
advertisement

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
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn