Which of the following keyword is used to define the function in javascript

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.

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.

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?

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()
{  
    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.

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.

Next Steps:

  • Get Free Certificate of Merit in JavaScript
  • Participate in JavaScript Certification Contest
  • Become a Top Ranker in JavaScript
  • Take JavaScript Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Which keyword is used to define function in JavaScript?

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). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

Which keyword is used to define a function?

The def keyword is used to create, (or define) a function.

What is function () () in JavaScript?

A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

Which of the following is used to create functions in JavaScript?

Using Function Constructor You can create a function using the Function constructor as shown in the following example. In the Function constructor, you pass parameters and function body as a string. The function created with the Function constructor is always created in the global scope.