Javascript (JS) Functions MCQ
This section focuses on the "Javascript Functions" of the Javascript. These Multiple Choice Questions (mcq) should be practiced to improve the Javascript skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Consider the JavaScript code:
Identify the value that will be displayed in alert box at line 5?
function lfc(myname){
console.log(10+"lfc" +12);
}
res=lfc(10);
console.log(res); //line 5
A. 10
B. 10lfc12
C. undefined
D. 10lfc
View Answer
Ans : C
Explanation: This option is correct because the function lfc() is not returning any value so it will be undefined.
2. Consider the JavaScript code:
Choose the correct code to be inserted at line 4 in order to invoke the function.
thisfun=function lfc(x){
console.log(x*x);
}
// line 4
A. thisfun(10)
B. lfc(10)
C. function thisfun(10)
D. None of the above
View Answer
Ans : A
Explanation: This option is correct because the function that is assigned to a variable can be called with the help of variable name only.
3. What will be the output of the below code?
function password(pass) {
for (i = 0; i < pass.length; i++) {
}
}
function name(pname) {
console.log("The value of i is "+i);
for (i = 0; i < pname.length; i++) {
}
}
password("54321");
name("John");
A. 0
B. 5
C. undefined
D. error
View Answer
Ans : B
Explanation: This option is correct because when the password("54321") is called the value of i will be incremented in each iteration and at the end its value will be 5. When name("John") function is called it will print the value of i as 5.
4. What will be the output of the below code?
let chaval= (p)=>p+2;
console.log(chaval(2));
A. 4
B. 22
C. Error: changeVal is not a function
D. undefined
View Answer
Ans : A
Explanation: This option A is correct because, the value 2 is passed as an input to the function it will get added and thus the output will be 4.
5. what will be the output of the below code?
num1=5;
function cal() {
num1=10;
num2=5;
num3=num2*num1;
console.log(num3);
}
cal();
A. 25
B. 50
C. 100
D. Error cannot have more than one variable with same name
View Answer
Ans : B
Explanation: This option B is correct because, when the function is called the value of num1 will become 20 and thus the value of num3 will be 200.
6. Functions are invoked as functions or as methods with an __________.
A. invocation statement
B. invocation expression
C. invocation function
D. invocation method
View Answer
Ans : B
Explanation: Functions are invoked as functions or as methods with an invocation expression.
7. ........ functions need not allow invocations with zero arguments.
A. zero
B. strict
C. empty
D. varargs
View Answer
Ans : D
Explanation: varargs functions need not allow invocations with zero arguments.
8. When you invoke the........ method on a function f and pass an object o, the method returns a new function.
A. apply()
B. call()
C. bind()
D. string()
View Answer
Ans : C
Explanation: When you invoke the bind() method on a function f and pass an object o, the method returns a new function.
9. Variable declared is example of ___________ Variable.
function message() {
var name = "Bench";
alert(name);
}
A. Local
B. Global
C. Semi-Global
D. None of the above
View Answer
Ans : A
Explanation: Variable declared is example of Local Variable.
10. What is the output for code A and B?
Code A:
var x = 10;
y = --x + 1;
alert(y);
Code B:
var x = 10;
y = x-- + 1;
alert(y);
A. 10,10
B. 10,11
C. 11,10
D. 11,11
View Answer
Ans : B
Explanation: 10,11 is the output for code A and B
11. A _________ is a group of reusable code which can be called anywhere in your program.
A. exception
B. function
C. loop
D. switch
View Answer
Ans : B
Explanation: A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.
12. The most common way to define a function in JavaScript is by using the ____________ keyword.
A. fun
B. var
C. function
D. define
View Answer
Ans : C
Explanation: Before we use a function, we need to define it. The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
13. Which statement required if you want to return a value from a function?
A. continue
B. break
C. loop
D. return
View Answer
Ans : D
Explanation: A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.
14. Does JavaScript allows us to write our own functions as well?
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, JavaScript allows us to write our own functions as well.
15. The Function() constructor expects ______ number of string arguments
A. 0
B. 1
C. 2
D. any
View Answer
Ans : D
Explanation: The Function() constructor expects any number of string arguments.
Also check :
Discussion