MCQs Of Day 7: Functions in JavaScript

Rashmi Mishra
0

 MCQs Of Day 7: Functions in JavaScript

MCQs on Functions in JavaScript


1.  What is the correct way to define a function in JavaScript?

o    a) function = myFunction() {}

o    b) function myFunction() {}

o    c) myFunction() function {}

o    d) function: myFunction() {}

Answer: b) function myFunction() {}
Explanation: Functions in JavaScript are defined using the function keyword followed by the function name, parentheses for parameters, and curly braces for the function body.


2.  Which of the following is true about JavaScript function expressions?

o    a) Functions can only be declared using the function keyword.

o    b) Function expressions can be assigned to variables.

o    c) Function expressions require a return statement.

o    d) Function expressions cannot have parameters.

Answer: b) Function expressions can be assigned to variables.
Explanation: Function expressions are functions defined as part of a larger expression, such as being assigned to variables. These are often anonymous functions.


3.  What is the output of the following code?

javascript

Copy code

function test() {

    console.log("Hello World");

}

test();

o    a) "test"

o    b) undefined

o    c) Hello World

o    d) "Hello World"

Answer: c) Hello World
Explanation: The function test() prints "Hello World" to the console when called.


4.  What will be the output of the following code?

javascript

Copy code

function sum(a, b) {

    return a + b;

}

console.log(sum(3, 4));

o    a) 7

o    b) undefined

o    c) "34"

o    d) Error

Answer: a) 7
Explanation: The function sum() returns the sum of a and b, which in this case is 7.


5.  Which of the following statements about function parameters is true?

o    a) Function parameters are always required.

o    b) Functions in JavaScript cannot have default parameters.

o    c) Functions can have optional parameters.

o    d) Functions in JavaScript cannot take any parameters.

Answer: c) Functions can have optional parameters.
Explanation: In JavaScript, parameters can be optional by using default values (e.g., function myFunc(x = 10) {...}).


6.  What is the purpose of the return statement in a JavaScript function?

o    a) To terminate the function immediately.

o    b) To assign a value to a variable.

o    c) To send a value back to the caller of the function.

o    d) To print something to the console.

Answer: c) To send a value back to the caller of the function.
Explanation: The return statement allows a function to return a value to the place where the function was called.


7.  What will the following function return?

javascript

Copy code

function greet(name) {

    return "Hello, " + name;

}

console.log(greet("Alice"));

o    a) Hello

o    b) Hello, Alice

o    c) Alice

o    d) undefined

Answer: b) Hello, Alice
Explanation: The function greet concatenates the string "Hello, " with the parameter name, resulting in "Hello, Alice".


8.  What happens if you call a function with fewer arguments than defined parameters?

o    a) The missing parameters are assigned null.

o    b) The missing parameters are assigned undefined.

o    c) The function throws an error.

o    d) The function ignores the missing parameters.

Answer: b) The missing parameters are assigned undefined.
Explanation: If fewer arguments are passed than the function has parameters, the missing parameters are assigned undefined.


9.  Which of the following is an example of a function expression?

o    a) function myFunction() { }

o    b) var myFunc = function() { }

o    c) function() { }

o    d) function myFunc() { return; }

Answer: b) var myFunc = function() { }
Explanation: A function expression is a function assigned to a variable. In this case, myFunc holds an anonymous function.


10.                   Which of the following correctly defines a function that takes no arguments?

  • a) function myFunction() {}
  • b) function myFunction(;) {}
  • c) function myFunction[] {}
  • d) function myFunction() => {}

Answer: a) function myFunction() {}
Explanation: The correct syntax for a function that takes no arguments is function myFunction() {}.


11.                   What does the following code print?

javascript

Copy code

function showMessage(message) {

    console.log(message);

}

showMessage("Good morning");

  • a) Good morning
  • b) message
  • c) undefined
  • d) Error

Answer: a) Good morning
Explanation: The function showMessage prints the value of the message parameter, which is "Good morning".


12.                   What will be the result of the following code?

javascript

Copy code

function add(a, b) {

    return a + b;

}

console.log(add(5, 10));

  • a) 510
  • b) 15
  • c) 5 + 10
  • d) undefined

Answer: b) 15
Explanation: The function add adds the two arguments 5 and 10, returning the sum 15.


13.                   What happens if a function does not have a return statement?

  • a) It will return null.
  • b) It will return undefined.
  • c) It will throw an error.
  • d) The function will stop execution.

Answer: b) It will return undefined.
Explanation: If a function does not have a return statement, JavaScript implicitly returns undefined.


14.                   How would you declare a function that takes three parameters in JavaScript?

  • a) function myFunc(x, y, z) {}
  • b) function myFunc(x, y, z;) {}
  • c) function myFunc(x, y) {}
  • d) function myFunc() { x, y, z }

Answer: a) function myFunc(x, y, z) {}
Explanation: This is the correct syntax to define a function that accepts three parameters.


15.                   Which of the following is a valid function expression in JavaScript?

  • a) var myFunc = function() { console.log("Hello!"); };
  • b) function myFunc() { console.log("Hello!"); };
  • c) myFunc = function() { console.log("Hello!"); };
  • d) Both a and c are correct.

Answer: d) Both a and c are correct.
Explanation: Both options are valid function expressions, where the function is either assigned to a variable or defined inline.


16.                   How do you define a function that is immediately invoked after being defined?

  • a) function() {}();
  • b) (function() {})()
  • c) function()() {};
  • d) () => function() {};

Answer: b) (function() {})()
Explanation: This is an Immediately Invoked Function Expression (IIFE), where the function is invoked right after being defined.


17.                   Which function declaration will work properly to accept two parameters and add them together?

  • a) function add(a, b) { return a + b; }
  • b) function add(a, b) { console.log(a + b); }
  • c) function add(a, b) { a + b; }
  • d) Both a and b are correct.

Answer: d) Both a and b are correct.
Explanation: Option a returns the sum, while option b prints the sum to the console. Both are valid ways to use the parameters.


18.                   Which of the following will cause a TypeError?

  • a) function test() { console.log("Hello"); }
  • b) var test = 5;
  • c) var test = function() { console.log("Hello"); }
  • d) function test() { return undefined; }

Answer: b) var test = 5;
Explanation: Assigning a number to a variable test would override the function declaration and cause a conflict if test is called later.


19.                   Which of the following statements will invoke a function named calculate with arguments 5 and 10?

  • a) calculate(5, 10);
  • b) calculate{5, 10};
  • c) function calculate(5, 10);
  • d) function calculate(5, 10) {}

Answer: a) calculate(5, 10);
Explanation: This is the correct way to invoke a function and pass arguments.


20.                   What does the following code output?

javascript

Copy code

function calculate(x, y) {

    return x * y;

}

console.log(calculate(4, 3));

  • a) 12
  • b) 7
  • c) 43
  • d) undefined

Answer: a) 12
Explanation: The function calculate multiplies 4 and 3, returning the result 12.


Remaining questions can continue with similar patterns involving understanding of function expressions, higher-order functions, closures, scope, and recursion. Let me know if you want the entire list!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here’s the continuation of the MCQs on JavaScript functions.


21.                   What is the default value of an undefined parameter in JavaScript?

  • a) null
  • b) undefined
  • c) 0
  • d) NaN

Answer: b) undefined
Explanation: If a parameter is not provided when a function is called, it is automatically assigned the value undefined.


22.                   What will be the result of the following code?

javascript

Copy code

function greet(name = "Guest") {

    console.log("Hello, " + name);

}

greet();

  • a) Hello, undefined
  • b) Hello, Guest
  • c) Hello
  • d) Error

Answer: b) Hello, Guest
Explanation: Since no argument is passed to the function, the default value "Guest" is used for the parameter name.


23.                   What is a function’s local scope in JavaScript?

  • a) Variables defined outside the function
  • b) Variables defined inside the function
  • c) Variables defined within a global scope
  • d) Variables defined in any outer function

Answer: b) Variables defined inside the function
Explanation: Variables declared inside a function are considered local to that function and cannot be accessed outside it.


24.                   Which of the following statements about global scope is true?

  • a) Variables in the global scope can only be accessed inside functions.
  • b) Global variables can be modified inside a function if declared without var, let, or const.
  • c) Global scope refers to only built-in objects and variables.
  • d) Global scope variables cannot be modified.

Answer: b) Global variables can be modified inside a function if declared without var, let, or const.
Explanation: If a variable is declared globally without the var, let, or const keyword, it becomes global, and its value can be modified within functions.


25.                   What will be the output of the following code?

javascript

Copy code

var x = 10;

function foo() {

    var x = 20;

    console.log(x);

}

foo();

  • a) 10
  • b) 20
  • c) undefined
  • d) Error

Answer: b) 20
Explanation: The x inside the function is a local variable, and the function foo() logs this local x, not the global x.


26.                   What will the following code output?

javascript

Copy code

var y = 30;

function bar() {

    console.log(y);

    var y = 40;

}

bar();

  • a) 30
  • b) undefined
  • c) 40
  • d) Error

Answer: b) undefined
Explanation: Due to hoisting, the variable y inside the function is initialized as undefined before the function is executed, so undefined is logged.


27.                   What is the result of calling the function foo() below?

javascript

Copy code

var foo = function() {

    return "Hello!";

}

console.log(foo());

  • a) Hello!
  • b) undefined
  • c) Error
  • d) null

Answer: a) Hello!
Explanation: The function is assigned to the variable foo, and when invoked, it returns "Hello!".


28.                   Which of the following is true about recursive functions in JavaScript?

  • a) A recursive function must have a base case.
  • b) A recursive function can never call itself.
  • c) A recursive function will always throw an error.
  • d) A recursive function cannot return a value.

Answer: a) A recursive function must have a base case.
Explanation: Recursive functions call themselves, but they must have a base case to stop the recursion and avoid infinite loops.


29.                   What is the output of the following code?

javascript

Copy code

function recursiveTest(n) {

    if (n === 0) return 0;

    return n + recursiveTest(n - 1);

}

console.log(recursiveTest(3));

  • a) 0
  • b) 6
  • c) 3
  • d) 10

Answer: b) 6
Explanation: The function sums all numbers from n down to 0, so 3 + 2 + 1 + 0 = 6.


30.                   Which of the following is NOT a valid way to define a function in JavaScript?

  • a) function myFunc() {}
  • b) var myFunc = function() {};
  • c) let myFunc = () => {};
  • d) function => myFunc() {}

Answer: d) function => myFunc() {}
Explanation: The correct syntax for arrow functions is let myFunc = () => {}. The option function => myFunc() is not valid.


31.                   What does the this keyword refer to inside a JavaScript function?

  • a) It refers to the global object in all functions.
  • b) It refers to the object that called the function.
  • c) It refers to the function itself.
  • d) It refers to the first parameter passed to the function.

Answer: b) It refers to the object that called the function.
Explanation: Inside a method, this refers to the object that is calling the method.


32.                   What is the output of the following code?

javascript

Copy code

function test() {

    console.log(this);

}

test();

  • a) The global object (window in browsers)
  • b) undefined
  • c) Error
  • d) The function itself

Answer: a) The global object (window in browsers)
Explanation: In non-strict mode, calling a function like this in the global context refers to the global object (in browsers, the window object).


33.                   How do you make a function available as a global function?

  • a) By declaring the function inside the global object.
  • b) By using var inside the function.
  • c) By declaring the function outside any other function.
  • d) Functions are automatically global in JavaScript.

Answer: c) By declaring the function outside any other function.
Explanation: Functions declared outside any function or block are available globally.


34.                   What will the following code output?

javascript

Copy code

function foo(x) {

    return function(y) {

        return x + y;

    }

}

var add5 = foo(5);

console.log(add5(3));

  • a) 8
  • b) 53
  • c) 5
  • d) Error

Answer: a) 8
Explanation: The function foo(5) returns a new function that adds x (which is 5) to y. When add5(3) is called, it adds 5 + 3 = 8.


35.                   What will the following code output?

javascript

Copy code

var counter = (function() {

    var count = 0;

    return function() {

        count++;

        return count;

    }

})();

console.log(counter());

console.log(counter());

  • a) 0, 1
  • b) 1, 2
  • c) 0, 0
  • d) Error

Answer: b) 1, 2
Explanation: The code uses an IIFE (Immediately Invoked Function Expression) to create a function that keeps track of the count variable. Each call to counter() increments the count and returns it.


36.                   Which of the following defines a function that returns a string "Hello, World!"?

  • a) function greet() { "Hello, World!"; }
  • b) function greet() { return "Hello, World!"; }
  • c) function greet() => { "Hello, World!"; }
  • d) function greet() => return "Hello, World!";

Answer: b) function greet() { return "Hello, World!"; }
Explanation: The return keyword must be used to return a value from the function.


37.                   What is the result of the following code?

javascript

Copy code

var foo = function() { return arguments[0]; }

console.log(foo(5, 10));

  • a) 5
  • b) 10
  • c) undefined
  • d) Error

Answer: a) 5
Explanation: The function returns the first argument passed, which is 5.


38.                   Which of the following options is true about closures?

  • a) A closure can only access variables within its own function.
  • b) A closure allows a function to access variables from an outer function even after the outer function has returned.
  • c) A closure cannot access variables from the global scope.
  • d) A closure has no access to its own local variables.

Answer: b) A closure allows a function to access variables from an outer function even after the outer function has returned.
Explanation: Closures allow an inner function to "remember" and access variables from its outer function, even after the outer function has finished execution.


Let me know if you'd like to continue or need further questions!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here’s the continuation of the MCQs for JavaScript functions:


39.                   What is the output of the following code?

javascript

Copy code

function multiply(a, b) {

    return a * b;

}

console.log(multiply(4, 5));

  • a) 4
  • b) 5
  • c) 20
  • d) Error

Answer: c) 20
Explanation: The function multiplies 4 and 5, returning 20.


40.                   What will happen if a function is called without passing the required parameters in JavaScript?

  • a) The function will throw an error.
  • b) The function will return undefined for missing parameters.
  • c) The function will return null for missing parameters.
  • d) The function will ignore the missing parameters.

Answer: b) The function will return undefined for missing parameters.
Explanation: If a function is called without passing an argument for a parameter, that parameter gets the value undefined.


41.                   What is the result of the following code?

javascript

Copy code

function test(a, b = 5) {

    console.log(a + b);

}

test(10);

  • a) 10
  • b) 15
  • c) 5
  • d) Error

Answer: b) 15
Explanation: The second argument b defaults to 5 when not passed, so 10 + 5 = 15.


42.                   What will the following code output?

javascript

Copy code

function foo(a, b, c) {

    console.log(a, b, c);

}

foo(1, 2);

  • a) 1 2 undefined
  • b) 1 2
  • c) Error
  • d) undefined undefined undefined

Answer: a) 1 2 undefined
Explanation: Since the function is called with two arguments, c remains undefined.


43.                   Which method can be used to invoke a function in JavaScript?

  • a) function.call()
  • b) function.invoke()
  • c) function()
  • d) function.exec()

Answer: c) function()
Explanation: The function is invoked by calling it with parentheses.


44.                   How can you pass a function as an argument to another function in JavaScript?

  • a) By assigning it to a variable.
  • b) By passing the function name as a string.
  • c) By passing the function directly without parentheses.
  • d) All of the above.

Answer: a) By assigning it to a variable.
Explanation: You can pass a function by assigning it to a variable and then passing that variable to another function.


45.                   What is the output of the following code?

javascript

Copy code

function bar() {

    var x = 2;

    return function() {

        return x + 3;

    }

}

var foo = bar();

console.log(foo());

  • a) 3
  • b) 5
  • c) 6
  • d) undefined

Answer: b) 5
Explanation: The bar() function returns a closure, which has access to the x variable, adding 3 to it, resulting in 5.


46.                   What does the function bind() do in JavaScript?

  • a) It binds a function to a particular event.
  • b) It allows you to create a new function with a specific this value and arguments.
  • c) It creates a closure.
  • d) It connects two functions together.

Answer: b) It allows you to create a new function with a specific this value and arguments.
Explanation: The bind() method is used to create a new function that, when invoked, has its this value set to a specific object.


47.                   Which of the following statements is true regarding function declarations and expressions?

  • a) Function declarations are hoisted, but function expressions are not.
  • b) Function expressions are hoisted, but function declarations are not.
  • c) Both function declarations and expressions are hoisted equally.
  • d) Neither function declarations nor expressions are hoisted.

Answer: a) Function declarations are hoisted, but function expressions are not.
Explanation: Function declarations are hoisted to the top of their scope, meaning they are available before they are defined, whereas function expressions are not hoisted.


48.                   What will the following code output?

javascript

Copy code

var multiply = function(a, b) {

    return a * b;

}

console.log(multiply(4, 3));

  • a) 7
  • b) 12
  • c) Error
  • d) NaN

Answer: b) 12
Explanation: The multiply function returns the result of multiplying 4 and 3, which is 12.


49.                   What does the arguments object in a function do?

  • a) It is an array-like object that contains all arguments passed to the function.
  • b) It stores the arguments passed to the function in the global scope.
  • c) It is used to define the number of arguments a function takes.
  • d) It stores arguments only for anonymous functions.

Answer: a) It is an array-like object that contains all arguments passed to the function.
Explanation: The arguments object allows you to access all the arguments passed to a function, even if you don't define them explicitly.


50.                   Which of the following is NOT a valid function parameter in JavaScript?

  • a) function foo(x, y, z) { ... }
  • b) function foo(x = 5, y) { ... }
  • c) function foo(...args) { ... }
  • d) function foo(5, y) { ... }

Answer: d) function foo(5, y) { ... }
Explanation: Function parameters should be identifiers (variable names), not literals like 5.


51.                   Which of the following is a way to define a function in JavaScript?

  • a) let func = function() {};
  • b) function func() {};
  • c) const func = () => {};
  • d) All of the above.

Answer: d) All of the above.
Explanation: Functions in JavaScript can be defined using a function declaration, function expression, or arrow function expression.


52.                   What will the following code output?

javascript

Copy code

function test() {

    console.log(arguments.length);

}

test(1, 2, 3);

  • a) 1
  • b) 2
  • c) 3
  • d) undefined

Answer: c) 3
Explanation: The arguments.length property returns the number of arguments passed to the function, which is 3.


53.                   What is the default value of parameters in a function in JavaScript?

  • a) null
  • b) 0
  • c) undefined
  • d) false

Answer: c) undefined
Explanation: If a function is called without passing a value for a parameter, the default value is undefined.


54.                   How would you define a function that returns the square of a number in JavaScript?

  • a) function square(n) { return n * n; }
  • b) function square(n) { return n ^ 2; }
  • c) function square(n) { n * n; }
  • d) function square(n) { n ** 2; }

Answer: a) function square(n) { return n * n; }
Explanation: The correct way to calculate the square of a number is by multiplying the number by itself: n * n.


55.                   What is the scope of a variable declared inside a function?

  • a) Global scope
  • b) Local scope
  • c) Block scope
  • d) None

Answer: b) Local scope
Explanation: Variables declared inside a function are local to that function and cannot be accessed outside of it.


Let me know if you want to continue or need more questions!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here’s the continuation of the MCQs for JavaScript functions:


56.                   Which of the following correctly defines a recursive function in JavaScript?

  • a) A function that calls itself within its body.
  • b) A function that loops over an array of values.
  • c) A function that calls another function.
  • d) A function that returns a value based on condition.

Answer: a) A function that calls itself within its body.
Explanation: A recursive function is one that calls itself in order to solve a problem, typically with a base case to terminate the recursion.


57.                   What will happen when the following function is called?

javascript

Copy code

function test(a, b) {

    return a + b;

}

console.log(test(2));

  • a) 2
  • b) NaN
  • c) Error
  • d) Undefined

Answer: b) NaN
Explanation: The function expects two arguments, but only one is passed, and JavaScript cannot add undefined to a number, returning NaN.


58.                   Which of the following statements is true about this keyword in JavaScript?

  • a) this always refers to the global object.
  • b) this always refers to the function that calls it.
  • c) The value of this is determined by how a function is called.
  • d) this can never be used in arrow functions.

Answer: c) The value of this is determined by how a function is called.
Explanation: The value of this is dynamic and depends on how a function is called, which can vary in different contexts.


59.                   Which method is used to invoke a function immediately after it's defined in JavaScript?

  • a) function()
  • b) IIFE (Immediately Invoked Function Expression)
  • c) call()
  • d) invoke()

Answer: b) IIFE (Immediately Invoked Function Expression)
Explanation: An IIFE is a function expression that is defined and called immediately.


60.                   What does the following code output?

javascript

Copy code

var x = 10;

function example() {

    var x = 20;

    console.log(x);

}

example();

  • a) 10
  • b) 20
  • c) Undefined
  • d) Error

Answer: b) 20
Explanation: The function example() has its own local variable x with the value 20, so when console.log(x) is called inside the function, it outputs 20.


61.                   What is the purpose of the return keyword in JavaScript functions?

  • a) It terminates the function and returns a value.
  • b) It stops the function execution without returning anything.
  • c) It defines the function parameters.
  • d) It loops the function repeatedly.

Answer: a) It terminates the function and returns a value.
Explanation: The return keyword is used to exit a function and optionally return a value to the calling code.


62.                   Which of the following is an example of a JavaScript function expression?

  • a) function sum(a, b) { return a + b; }
  • b) var sum = function(a, b) { return a + b; };
  • c) function sum(a, b) { return a + b; }
  • d) const sum = () => a + b;

Answer: b) var sum = function(a, b) { return a + b; };
Explanation: A function expression is when a function is assigned to a variable. In this case, the function is assigned to sum.


63.                   Which statement is correct regarding arrow functions in JavaScript?

  • a) Arrow functions must have curly braces around their body.
  • b) Arrow functions automatically bind the this value to the function.
  • c) Arrow functions cannot have parameters.
  • d) Arrow functions are slower than regular functions.

Answer: b) Arrow functions automatically bind the this value to the function.
Explanation: Arrow functions inherit the this value from their surrounding context, unlike regular functions that determine this based on how they are called.


64.                   What will the following code output?

javascript

Copy code

function greet(name = "Guest") {

    console.log("Hello " + name);

}

greet("Alice");

greet();

  • a) Hello Guest, Hello Guest
  • b) Hello Alice, Hello Alice
  • c) Hello Alice, Hello Guest
  • d) Error

Answer: c) Hello Alice, Hello Guest
Explanation: The first call passes "Alice", so it logs Hello Alice, and the second call uses the default value "Guest", logging Hello Guest.


65.                   What is the correct way to define an anonymous function in JavaScript?

  • a) function() { return "Hello"; }
  • b) var func = function() { return "Hello"; };
  • c) const func = () => { return "Hello"; };
  • d) All of the above.

Answer: d) All of the above.
Explanation: Anonymous functions can be defined in all three ways: function expressions, arrow functions, and without a name.


66.                   How does a function return a value in JavaScript?

  • a) Using the return statement.
  • b) Using function keyword.
  • c) Using console.log().
  • d) Using this keyword.

Answer: a) Using the return statement.
Explanation: A function in JavaScript returns a value using the return keyword, which sends the result back to the caller.


67.                   Which of the following is a valid function declaration in JavaScript?

  • a) function sum() { return a + b; }
  • b) let sum = function() { return a + b; };
  • c) const sum = () => a + b;
  • d) All of the above.

Answer: d) All of the above.
Explanation: All the options are valid, but they define functions in different ways: traditional function declaration, function expression, and arrow function.


68.                   What is a key characteristic of the arguments object in JavaScript?

  • a) It is an array-like object.
  • b) It stores values returned from the function.
  • c) It can be used only in regular functions, not arrow functions.
  • d) It is used to define the function parameters.

Answer: a) It is an array-like object.
Explanation: The arguments object is array-like and holds all the arguments passed to a function, though it is not a true array.


69.                   What will the following code output?

javascript

Copy code

function multiply(x, y) {

    return x * y;

}

console.log(multiply(3, 4));

  • a) 7
  • b) 12
  • c) Error
  • d) undefined

Answer: b) 12
Explanation: The function multiplies 3 and 4, returning 12.


70.                   Which of the following is an example of a function that does not take any parameters?

  • a) function greet(name) { console.log(name); }
  • b) function sayHello() { console.log("Hello!"); }
  • c) function sum(a, b) { return a + b; }
  • d) function multiply(a, b, c) { return a * b * c; }

Answer: b) function sayHello() { console.log("Hello!"); }
Explanation: The sayHello function does not take any parameters.


Let me know if you'd like me to continue or need any specific changes!

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs related to JavaScript functions:


71.                   What is the output of the following code?

javascript

Copy code

function add(a, b) {

    return a + b;

}

console.log(add(5, "5"));

  • a) 55
  • b) 10
  • c) NaN
  • d) Error

Answer: a) 55
Explanation: JavaScript performs type coercion. When a number is added to a string, JavaScript converts the number to a string, resulting in "55".


72.                   Which of the following is true about JavaScript functions?

  • a) Functions in JavaScript cannot be assigned to variables.
  • b) Functions can be passed as arguments to other functions.
  • c) Functions in JavaScript must always have return statements.
  • d) JavaScript functions can only return strings.

Answer: b) Functions can be passed as arguments to other functions.
Explanation: Functions in JavaScript are first-class citizens, meaning they can be passed as arguments to other functions, returned from functions, and assigned to variables.


73.                   Which of the following is the correct syntax to define a named function in JavaScript?

  • a) function myFunction { return "Hello"; }
  • b) function myFunction() { return "Hello"; }
  • c) var myFunction() { return "Hello"; }
  • d) const myFunction = function() { return "Hello"; }

Answer: b) function myFunction() { return "Hello"; }
Explanation: This is the correct syntax for defining a named function in JavaScript.


74.                   What will be the output of the following code?

javascript

Copy code

let result = (function() {

    return 3 * 2;

})();

console.log(result);

  • a) 3 * 2
  • b) 6
  • c) NaN
  • d) Error

Answer: b) 6
Explanation: This is an example of an immediately invoked function expression (IIFE) that calculates 3 * 2 and returns 6.


75.                   Which of the following JavaScript function types cannot have a return value?

  • a) Regular function
  • b) Arrow function
  • c) Anonymous function
  • d) Void function

Answer: d) Void function
Explanation: A void function is one that does not return a value. It is typically used when no value is required from the function.


76.                   What is the scope of a variable declared within a function in JavaScript?

  • a) Global scope
  • b) Function scope
  • c) Block scope
  • d) Window scope

Answer: b) Function scope
Explanation: A variable declared within a function has function scope, meaning it is accessible only within that function.


77.                   What will be the output of the following code?

javascript

Copy code

var x = 10;

function changeValue() {

    var x = 20;

    console.log(x);

}

changeValue();

console.log(x);

  • a) 10 10
  • b) 20 10
  • c) 20 20
  • d) Error

Answer: b) 20 10
Explanation: The changeValue function has a local variable x with a value of 20, which is logged inside the function. The global variable x is still 10, which is logged after the function call.


78.                   What will happen if you call a function before it is defined in JavaScript?

  • a) The function call will throw an error.
  • b) JavaScript will automatically define the function and call it.
  • c) The function will be called successfully as long as it's declared using var.
  • d) The function will be called successfully only if it’s a function expression.

Answer: c) The function will be called successfully as long as it's declared using var.
Explanation: JavaScript hoists function declarations, meaning they are available even before they are written in the code. However, function expressions are not hoisted.


79.                   Which of the following can be a parameter of a JavaScript function?

  • a) Variables
  • b) Expressions
  • c) Functions
  • d) All of the above

Answer: d) All of the above
Explanation: In JavaScript, functions can take variables, expressions, and even other functions as parameters.


80.                   How can you make sure that a function will always return a value in JavaScript?

  • a) By using a return statement at the end of the function.
  • b) By ensuring there are no conditions in the function.
  • c) By always using a console.log() inside the function.
  • d) By defining the function as a void function.

Answer: a) By using a return statement at the end of the function.
Explanation: To ensure that a function always returns a value, a return statement must be used inside the function.


81.                   Which of the following is a feature of JavaScript functions?

  • a) They can return only one value.
  • b) They cannot modify global variables.
  • c) They can be invoked with different numbers of arguments.
  • d) They must always return a value.

Answer: c) They can be invoked with different numbers of arguments.
Explanation: JavaScript functions are flexible and can be invoked with a variable number of arguments.


82.                   What is the correct syntax for passing a function as an argument to another function?

  • a) function(x) { return x * 2; }(function(x) { return x * 2; });
  • b) myFunction(function(x) { return x * 2; });
  • c) function(x) { return x * 2; } => myFunction();
  • d) function() { return x * 2; } to myFunction();

Answer: b) myFunction(function(x) { return x * 2; });
Explanation: This shows the correct way of passing a function as an argument to another function.


83.                   Which of the following is a correct way to define a default parameter for a function in JavaScript?

  • a) function greet(name = "Guest") { console.log(name); }
  • b) function greet(name, "Guest") { console.log(name); }
  • c) function greet(name: "Guest") { console.log(name); }
  • d) function greet(name?) { console.log(name); }

Answer: a) function greet(name = "Guest") { console.log(name); }
Explanation: Default parameters are defined by assigning a value to a parameter in the function definition, as shown in the correct syntax.


84.                   Which of the following is true about function declarations in JavaScript?

  • a) They must be assigned to a variable.
  • b) They can be invoked before their definition due to hoisting.
  • c) They can only be invoked after they are defined.
  • d) They cannot have parameters.

Answer: b) They can be invoked before their definition due to hoisting.
Explanation: JavaScript hoists function declarations, so they can be invoked before their actual position in the code.


85.                   What happens if a JavaScript function does not have a return statement?

  • a) It throws an error.
  • b) It returns null.
  • c) It returns undefined.
  • d) It returns a blank string.

Answer: c) It returns undefined.
Explanation: If a function does not explicitly return a value, JavaScript returns undefined by default.


Let me know if you'd like more or need further explanations!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs on the concept of functions in JavaScript:


86.                   Which of the following is true about function expressions in JavaScript?

  • a) They can be invoked before they are defined.
  • b) They are assigned to variables and can be anonymous.
  • c) They must be named functions.
  • d) They cannot have return values.

Answer: b) They are assigned to variables and can be anonymous.
Explanation: Function expressions are functions that can be assigned to variables, and they can be anonymous (without a name).


87.                   What will be the output of the following code?

javascript

Copy code

function multiply(a, b = 2) {

    return a * b;

}

console.log(multiply(5));

  • a) 10
  • b) 7
  • c) NaN
  • d) Error

Answer: a) 10
Explanation: The function multiply has a default parameter b = 2. Since no second argument is passed, b takes the default value of 2, and the result is 5 * 2 = 10.


88.                   What is the value of result after the following code executes?

javascript

Copy code

function test() {

    var x = 20;

    if (true) {

        var x = 30;

    }

    return x;

}

console.log(test());

  • a) 20
  • b) 30
  • c) undefined
  • d) Error

Answer: b) 30
Explanation: The variable x is declared with var, which has function scope, not block scope. Therefore, the value of x is updated within the function to 30, and test() returns 30.


89.                   Which of the following is a feature of JavaScript functions?

  • a) Functions cannot be passed as arguments.
  • b) Functions can return multiple values.
  • c) Functions can have default parameters.
  • d) Functions can only be invoked once.

Answer: c) Functions can have default parameters.
Explanation: JavaScript allows defining default parameters for functions, so if a value is not provided for the parameter, the default value is used.


90.                   What is the result of the following function call?

javascript

Copy code

function sum(a, b) {

    return a + b;

}

console.log(sum(1));

  • a) NaN
  • b) 1
  • c) undefined
  • d) Error

Answer: a) NaN
Explanation: Since only one argument is passed, b is undefined. When a + b is evaluated, it results in 1 + undefined, which is NaN.


91.                   What will be the output of the following code?

javascript

Copy code

var x = 5;

function demo() {

    x = 10;

    console.log(x);

}

demo();

console.log(x);

  • a) 10 5
  • b) 5 10
  • c) 10 10
  • d) Error

Answer: c) 10 10
Explanation: Inside the function, x is updated to 10, and because the variable x is not declared locally (using var, let, or const), it updates the global variable x. Both console.log(x) calls output 10.


92.                   What is the output of the following code?

javascript

Copy code

function greet(name = "Guest") {

    console.log("Hello, " + name);

}

greet("Alice");

greet();

  • a) Hello, Alice Hello, Guest
  • b) Hello, Alice Hello, Alice
  • c) Hello, Guest Hello, Alice
  • d) Error

Answer: a) Hello, Alice Hello, Guest
Explanation: The first call passes the argument "Alice", so the function prints "Hello, Alice". The second call uses the default parameter "Guest", so it prints "Hello, Guest".


93.                   What does the following code do?

javascript

Copy code

function outer() {

    var x = 5;

    function inner() {

        console.log(x);

    }

    inner();

}

outer();

  • a) Prints undefined
  • b) Prints 5
  • c) Throws a ReferenceError
  • d) Does nothing

Answer: b) Prints 5
Explanation: The inner function has access to the variable x from the outer function due to closures. Therefore, it prints 5.


94.                   Which of the following JavaScript functions can be used as an event handler for a button click?

  • a) addEventListener()
  • b) click()
  • c) onClick()
  • d) handleClick()

Answer: a) addEventListener()
Explanation: addEventListener() is used to attach event listeners (such as a button click) to HTML elements.


95.                   How can you define a function expression in JavaScript?

  • a) function name() { console.log("Hello"); }
  • b) const name = function() { console.log("Hello"); };
  • c) name = function() { console.log("Hello"); }
  • d) name() => console.log("Hello");

Answer: b) const name = function() { console.log("Hello"); };
Explanation: This is the correct syntax for a function expression. The function is assigned to the name variable.


96.                   Which keyword is used to prevent a variable from being changed in JavaScript?

  • a) const
  • b) var
  • c) let
  • d) static

Answer: a) const
Explanation: The const keyword creates a constant variable, which cannot be reassigned after its initial value.


97.                   What does the following code output?

javascript

Copy code

function getFullName(firstName, lastName = "Doe") {

    return firstName + " " + lastName;

}

console.log(getFullName("John"));

  • a) John Doe
  • b) John
  • c) John undefined
  • d) Error

Answer: a) John Doe
Explanation: Since no lastName is passed, it uses the default value "Doe". Therefore, the function returns "John Doe".


98.                   What will be the value of y after the following code runs?

javascript

Copy code

let x = 10;

function test() {

    var y = 5;

    return y;

}

console.log(test());

console.log(y);

  • a) 5, 5
  • b) 5, undefined
  • c) undefined, undefined
  • d) Error, undefined

Answer: b) 5, undefined
Explanation: The function test() returns 5, which is logged. However, the variable y is locally scoped to the function, so trying to log y outside the function results in undefined.


99.                   Which of the following best describes the term "closure" in JavaScript?

  • a) A function that does not take parameters.
  • b) A function that does not return anything.
  • c) A function that has access to variables from its outer function.
  • d) A function that is called inside another function.

Answer: c) A function that has access to variables from its outer function.
Explanation: A closure is formed when a function retains access to variables from its outer (enclosing) function even after the outer function has returned.


100.            What is the purpose of the arguments object in JavaScript functions?

  • a) It allows you to pass an undefined number of arguments.
  • b) It stores the return value of a function.
  • c) It provides access to all arguments passed to a function.
  • d) It defines default values for function parameters.

Answer: c) It provides access to all arguments passed to a function.
Explanation: The arguments object is an array-like object that holds all arguments passed to a function, regardless of how many were actually defined in the function.



Tags

Post a Comment

0Comments

Post a Comment (0)