JavaScript Functions MCQs

1 min read

Functions are the primary building blocks of JavaScript applications, enabling code modularity, encapsulation, and reuse. In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned from function calls. Modern ECMAScript specifications (ES6+) expanded functional syntax beyond traditional declarations and expressions to include arrow functions (() => {}), default parameter values, and rest parameter syntax (...args). Mastering execution contexts, lexical scoping, closures, hoisting behavior, and explicit binding methods (call, apply, bind) is critical for writing predictable asynchronous or synchronous code and succeeding in technical interviews.

1. What is the output of the following code snippet? console.log(foo()); function foo() { return "Hello"; }

a) Hello
b) TypeError: foo is not a function
c) ReferenceError: foo is not defined
d) undefined
Correct Answer: a) Hello
Explanation:
Function declarations are fully hoisted to the top of their containing scope, allowing them to be invoked prior to their definition line.

2. What will happen when executing the following code snippet? console.log(bar()); var bar = function() { return "World"; };

a) World
b) TypeError: bar is not a function
c) ReferenceError: bar is not defined
d) undefined
Correct Answer: b) TypeError: bar is not a function
Explanation:
The variable declaration 'var bar' is hoisted as undefined. Attempting to invoke undefined as a function raises a TypeError.

3. What is the primary characteristic of a higher-order function in JavaScript?

a) It can only be defined inside an asynchronous function.
b) It takes one or more functions as arguments or returns a function.
c) It executes automatically upon page load.
d) It lacks access to local variables.
Correct Answer: b) It takes one or more functions as arguments or returns a function.
Explanation:
A higher-order function is any function that operates on other functions by accepting them as parameters, returning them, or both.

4. What is the output of the following code? function sum(...numbers) { return numbers.reduce((acc, curr) => acc + curr, 0); } console.log(sum(1, 2, 3, 4));

a) 10
b) 1
c) [1, 2, 3, 4]
d) NaN
Correct Answer: a) 10
Explanation:
The rest parameter ...numbers captures all arguments as an array [1, 2, 3, 4], which reduce then sums to 10.

5. What will be printed to the console? function show(a, b = a) { console.log(a + b); } show(5);

a) NaN
b) 5
c) 10
d) TypeError
Correct Answer: c) 10
Explanation:
Default parameters can reference preceding parameters. Here 'b' defaults to 'a' (5), so 5 + 5 evaluates to 10.

6. What is the output of the following code snippet? function test(x = y, y = 2) { return x + y; } console.log(test(undefined, 3));

a) 5
b) NaN
c) ReferenceError: Cannot access 'y' before initialization
d) undefined
Correct Answer: c) ReferenceError: Cannot access 'y' before initialization
Explanation:
Default parameters are evaluated left-to-right in their temporal dead zone. Accessing 'y' before its initialization line throws a ReferenceError.

7. What will be logged by the following code? const obj = { num: 42, getNum: function() { return this.num; } }; const unbound = obj.getNum; console.log(unbound());

a) 42
b) undefined
c) TypeError
d) null
Correct Answer: b) undefined
Explanation:
When unbound() is invoked without an explicit receiver object, its 'this' defaults to global (or undefined in strict mode), missing 'num'.

8. How can you permanently set the 'this' context of unbound() from the previous question to 'obj'?

a) const bound = unbound.bind(obj);
b) const bound = unbound.call(obj);
c) const bound = unbound.apply(obj);
d) const bound = unbound.setThis(obj);
Correct Answer: a) const bound = unbound.bind(obj);
Explanation:
Function.prototype.bind creates a new bound function that locks its 'this' binding to the provided target object.

9. What is the result of invoking Function.prototype.apply() with arguments?

a) Arguments must be passed as comma-separated values.
b) Arguments must be passed as a single array or array-like object.
c) It converts arguments into a string.
d) It returns a new un-invoked function.
Correct Answer: b) Arguments must be passed as a single array or array-like object.
Explanation:
apply() invokes a function immediately, taking 'this' as its first parameter and an array of arguments as its second parameter.

10. What will be printed to the console? function createCounter() { let count = 0; return { inc: () => ++count, get: () => count }; } const c = createCounter(); c.inc(); c.inc(); console.log(c.get());

a) 0
b) 1
c) 2
d) undefined
Correct Answer: c) 2
Explanation:
Both arrow methods share a closure over the same 'count' variable, so incrementing it twice sets count to 2.

11. What is the output of evaluating `typeof function(){}`?

a) "object"
b) "function"
c) "undefined"
d) "symbol"
Correct Answer: b) "function"
Explanation:
The typeof operator explicitly identifies callable function objects as "function".

12. What is the output of evaluating `typeof (() => {})`?

a) "object"
b) "function"
c) "arrow"
d) "undefined"
Correct Answer: b) "function"
Explanation:
Arrow functions are function instances, so typeof returns "function".

13. Which of the following describes how arrow functions handle the `new` keyword?

a) They instantiate empty objects.
b) They throw a TypeError because they cannot be used as constructors.
c) They automatically call Super().
d) They bind 'this' to global instead of the new instance.
Correct Answer: b) They throw a TypeError because they cannot be used as constructors.
Explanation:
Arrow functions lack a [[Construct]] internal method and prototype property, raising a TypeError if called with 'new'.

14. What is the value of `func.length` for `function func(a, b = 1, c) {}`?

a) 3
b) 1
c) 2
d) 0
Correct Answer: b) 1
Explanation:
The length property of a function specifies the number of expected parameters up to the first parameter with a default value.

15. What will be printed to the console? (function(x) { return (function(y) { console.log(x); })(2); })(1);

a) 1
b) 2
c) undefined
d) ReferenceError
Correct Answer: a) 1
Explanation:
The inner IIFE forms a closure accessing 'x' from its outer scope, logging 1.

16. What is the output of this code? function sayHi() { console.log(name); console.log(age); var name = "Alice"; let age = 25; }

a) Alice followed by 25
b) undefined followed by ReferenceError
c) ReferenceError followed by ReferenceError
d) undefined followed by undefined
Correct Answer: b) undefined followed by ReferenceError
Explanation:
'var name' is hoisted as undefined. 'let age' is hoisted in the Temporal Dead Zone, throwing a ReferenceError when accessed early.

17. What is the output of the following function expression? const add = function fn(a, b) { return a + b; }; console.log(typeof fn);

a) "function"
b) "undefined"
c) "object"
d) ReferenceError
Correct Answer: b) "undefined"
Explanation:
Named function expressions bind their name exclusively inside their own local body scope, leaving 'fn' undefined outside.

18. What will be printed to the console? const add = function fn(a, b) { return typeof fn; }; console.log(add(1, 2));

a) "function"
b) "undefined"
c) "object"
d) TypeError
Correct Answer: a) "function"
Explanation:
Inside the function body, the identifier 'fn' refers to the function itself, returning "function".

19. Which statement about pure functions is true?

a) They produce different outputs for identical inputs depending on global state.
b) They modify global variables directly.
c) They produce the same output for identical inputs and have no side effects.
d) They must always be written using arrow syntax.
Correct Answer: c) They produce the same output for identical inputs and have no side effects.
Explanation:
Pure functions depend solely on their parameters and create zero side effects outside their local scope.

20. What will be the output of this code? function foo(a, b) { arguments[0] = 10; console.log(a); } foo(1, 2);

a) 1
b) 10
c) undefined
d) TypeError
Correct Answer: b) 10
Explanation:
In non-strict mode, parameters in standard functions map dynamically to the arguments object slots.

21. What will be the output of this code in strict mode? "use strict"; function foo(a, b) { arguments[0] = 10; console.log(a); } foo(1, 2);

a) 1
b) 10
c) undefined
d) TypeError
Correct Answer: a) 1
Explanation:
In strict mode, the arguments object is decoupled from formal parameter variables, keeping 'a' equal to 1.

22. What will be printed by the following code snippet? const person = { name: "Bob", greet: function() { return () => console.log(this.name); } }; person.greet()();

a) Bob
b) undefined
c) TypeError
d) null
Correct Answer: a) Bob
Explanation:
The arrow function inherits 'this' lexically from person.greet()'s context, where 'this' points to person.

23. What is function currying in JavaScript?

a) Converting a multi-argument function into a sequence of functions taking one argument each.
b) Wrapping a function in a try-catch block automatically.
c) Binding a function to execute repeatedly at intervals.
d) Executing a function in a separate thread.
Correct Answer: a) Converting a multi-argument function into a sequence of functions taking one argument each.
Explanation:
Currying transforms f(a, b, c) into callable nested functions f(a)(b)(c).

24. Which output is logged by this curried function? const multiply = a => b => c => a * b * c; console.log(multiply(2)(3)(4));

a) 24
b) 9
c) undefined
d) TypeError
Correct Answer: a) 24
Explanation:
Each nested arrow function captures its argument via closure: 2 * 3 * 4 = 24.

25. What will be logged by the following code? let x = 10; function outer() { let x = 20; function inner() { console.log(x); } return inner; } const fn = outer(); fn();

a) 10
b) 20
c) undefined
d) ReferenceError
Correct Answer: b) 20
Explanation:
Lexical scoping rules look up variables from the inner function's static definition environment, capturing local x = 20.

26. What will be printed to the console? function test() { console.log(this); } test.call(null);

a) null
b) window/global object (in non-strict mode)
c) undefined
d) TypeError
Correct Answer: b) window/global object (in non-strict mode)
Explanation:
In non-strict mode, passing null or undefined to call/apply replaces 'this' with the global object.

27. What will be printed to the console in strict mode? "use strict"; function test() { console.log(this); } test.call(null);

a) null
b) window/global object
c) undefined
d) TypeError
Correct Answer: a) null
Explanation:
In strict mode, call/apply retains the exact 'this' argument passed, remaining null.

28. What is the return value of an async function in JavaScript?

a) The raw returned value
b) A Promise resolving to the returned value
c) A Generator object
d) undefined
Correct Answer: b) A Promise resolving to the returned value
Explanation:
Async functions always wrap their return values inside a resolved Promise.

29. What will be logged by this code? async function foo() { return 42; } foo().then(val => console.log(val));

a) 42
b) Promise { 42 }
c) undefined
d) NaN
Correct Answer: a) 42
Explanation:
foo() returns a resolved Promise containing 42, which .then() receives as 'val'.

30. Which keyword pauses async function execution until a Promise resolves?

a) pause
b) yield
c) await
d) defer
Correct Answer: c) await
Explanation:
The await operator pauses async function execution until the target Promise settles.

31. What happens if a function constructor `function Foo() { this.x = 10; return { x: 20 }; }` is invoked with `new Foo()`?

a) Returns { x: 10 }
b) Returns { x: 20 }
c) Throws TypeError
d) Returns undefined
Correct Answer: b) Returns { x: 20 }
Explanation:
When a constructor explicitly returns a non-null object, that object overrides the newly constructed instance.

32. What happens if a function constructor `function Foo() { this.x = 10; return 5; }` is invoked with `new Foo()`?

a) Returns 5
b) Returns { x: 10 }
c) Throws TypeError
d) Returns undefined
Correct Answer: b) Returns { x: 10 }
Explanation:
If a constructor explicitly returns a primitive value (like 5), the primitive is ignored and the created instance is returned.

33. What is the output of the following code? function check() { return arguments instanceof Array; } console.log(check(1, 2));

a) true
b) false
c) TypeError
d) undefined
Correct Answer: b) false
Explanation:
The 'arguments' object is an array-like object with length and indexed elements, but it is not an instance of Array.

34. How can you convert the 'arguments' object into a real Array?

a) Array.from(arguments)
b) [...arguments]
c) Array.prototype.slice.call(arguments)
d) All of the above
Correct Answer: d) All of the above
Explanation:
Array.from(), spread syntax, and slice.call() are all valid techniques to transform array-like objects into true arrays.

35. What is a recursive function in JavaScript?

a) A function that invokes itself.
b) A function that executes inside a loop only.
c) A function without a return statement.
d) A function that takes another function as a parameter.
Correct Answer: a) A function that invokes itself.
Explanation:
A recursive function resolves problems by repeatedly calling itself until reaching a base condition.

36. What error is thrown when a recursive function lacks a valid base condition?

a) TypeError
b) RangeError: Maximum call stack size exceeded
c) ReferenceError
d) SyntaxError
Correct Answer: b) RangeError: Maximum call stack size exceeded
Explanation:
Unbounded recursion fills the browser execution call stack, leading to a maximum call stack size RangeError.

37. What will be logged by this code? const fn = (a, b) => a + b; console.log(fn.name);

a) "fn"
b) "anonymous"
c) "undefined"
d) ""
Correct Answer: a) "fn"
Explanation:
ES6 assigns automatic name inference to anonymous functions assigned to variables.

38. What is the output of the following generator function? function* gen() { yield 1; yield 2; } const g = gen(); console.log(g.next().value); console.log(g.next().value);

a) 1 followed by 2
b) 2 followed by 1
c) undefined followed by undefined
d) { value: 1 } followed by { value: 2 }
Correct Answer: a) 1 followed by 2
Explanation:
Calling g.next() yields objects containing { value, done }. Accessing .value retrieves 1 on first call and 2 on second call.

39. What does calling `g.next()` on a completed generator return?

a) { value: undefined, done: true }
b) { value: null, done: true }
c) undefined
d) TypeError
Correct Answer: a) { value: undefined, done: true }
Explanation:
Once a generator exhausts its yield statements, subsequent .next() invocations return { value: undefined, done: true }.

40. What will be printed to the console? function test() { console.log(a); var a = 10; } test();

a) 10
b) undefined
c) ReferenceError
d) null
Correct Answer: b) undefined
Explanation:
The declaration 'var a' is hoisted to the top of the function scope, but its value remains undefined until assignment.

41. What will be printed to the console? function test() { console.log(a); let a = 10; } test();

a) 10
b) undefined
c) ReferenceError
d) null
Correct Answer: c) ReferenceError
Explanation:
Variables declared with 'let' remain in the Temporal Dead Zone (TDZ) prior to declaration, throwing a ReferenceError.

42. What is the difference between function parameters and function arguments?

a) Parameters are values passed upon call; arguments are names in definition.
b) Parameters are names defined in function signature; arguments are real values passed during invocation.
c) There is no difference.
d) Parameters apply to arrow functions; arguments apply to standard functions.
Correct Answer: b) Parameters are names defined in function signature; arguments are real values passed during invocation.
Explanation:
Parameters are placeholder variable names listed in function headers; arguments are actual values supplied when calling.

43. What will be logged by the following snippet? const obj = { x: 10, fn() { return function() { return this.x; }; } }; console.log(obj.fn()());

a) 10
b) undefined
c) TypeError
d) null
Correct Answer: b) undefined
Explanation:
The inner function expression loses the 'obj' binding when invoked as a standalone function call `()`, so this.x is undefined.

44. How can you fix the previous snippet using an arrow function so it logs 10?

a) fn() { return () => this.x; }
b) fn: () => () => this.x
c) fn() { return function() { return this.x; }.bind(this); }
d) Both a and c
Correct Answer: d) Both a and c
Explanation:
Arrow functions lexically inherit 'this' from fn(), and .bind(this) explicitly preserves 'this'.

45. What will be logged to the console? function foo(a, b = 2, c = 3) {} console.log(foo.length);

a) 3
b) 1
c) 2
d) 0
Correct Answer: b) 1
Explanation:
function.length counts parameters before the first default parameter, returning 1.

46. What will be logged to the console? function foo(a = 1, b, c) {} console.log(foo.length);

a) 3
b) 0
c) 2
d) 1
Correct Answer: b) 0
Explanation:
Since the very first parameter 'a' has a default value, length counts 0 parameters prior to it.

47. What is the output of the following code snippet? let a = 1; function outer() { let a = 2; return new Function("return a;"); } console.log(outer()());

a) 1
b) 2
c) undefined
d) ReferenceError
Correct Answer: a) 1
Explanation:
Functions created with the `Function` constructor are always evaluated in global scope, referencing global a = 1.

48. What will be logged to the console? function say(msg) { console.log(this.name + ": " + msg); } const user = { name: "Alice" }; const boundSay = say.bind(user, "Hello"); boundSay("World");

a) Alice: Hello
b) Alice: World
c) Alice: Hello World
d) undefined: Hello
Correct Answer: a) Alice: Hello
Explanation:
bind() pre-binds parameters (partial application). Here "Hello" binds to parameter 'msg', ignoring subsequent arguments passed to boundSay.

49. What is memoization in relation to JavaScript functions?

a) Caching function results based on arguments to speed up future executions.
b) Converting functions to string representations.
c) Automatically deleting unused functions from memory.
d) Binding functions to DOM events.
Correct Answer: a) Caching function results based on arguments to speed up future executions.
Explanation:
Memoization is an optimization technique that stores computed results of pure functions for repeated parameter values.

50. What will be printed to the console? (function() { var a = b = 3; })(); console.log(typeof a, typeof b);

a) "undefined" "number"
b) "number" "number"
c) "undefined" "undefined"
d) "number" "undefined"
Correct Answer: a) "undefined" "number"
Explanation:
b = 3 creates an unintended global variable 'b' (in non-strict mode), while 'var a' is scoped to the IIFE. typeof a is "undefined" and typeof b is "number".

51. What will happen in strict mode for the code in the previous question? "use strict"; (function() { var a = b = 3; })();

a) ReferenceError: b is not defined
b) Runs normally without errors
c) TypeError
d) SyntaxError
Correct Answer: a) ReferenceError: b is not defined
Explanation:
In strict mode, assigning a value to an undeclared variable (b = 3) throws a ReferenceError.

52. What will be logged by the code snippet below? function foo() { return bar(); function bar() { return "Inside"; } } console.log(foo());

a) Inside
b) TypeError
c) undefined
d) ReferenceError
Correct Answer: a) Inside
Explanation:
Function declaration bar() is hoisted above the return statement in foo(), allowing execution.

53. What will be logged by the code snippet below? function foo() { return bar(); var bar = function() { return "Inside"; }; } console.log(foo());

a) Inside
b) TypeError: bar is not a function
c) undefined
d) ReferenceError
Correct Answer: b) TypeError: bar is not a function
Explanation:
'var bar' is hoisted as undefined. Invoking return bar() before assignment attempts to call undefined, throwing a TypeError.

54. What is the default value of the rest parameter `...args` when no matching arguments are passed?

a) An empty array []
b) undefined
c) null
d) TypeError
Correct Answer: a) An empty array []
Explanation:
When no matching arguments are provided, rest parameters evaluate to an empty array [].

55. What is the output of the following code snippet? function getArgs(...args) { return typeof args; } console.log(getArgs());

a) "object"
b) "array"
c) "undefined"
d) "function"
Correct Answer: a) "object"
Explanation:
Rest parameters collect arguments into an Array instance. In JavaScript, typeof arrays yields "object".

56. What is the output of the following code snippet? function getArgs(...args) { return Array.isArray(args); } console.log(getArgs());

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
Array.isArray correctly identifies the rest parameter ...args as a true Array instance.

57. Which statement correctly describes a callback function?

a) A function passed as an argument to another function to be executed later.
b) A function that returns another function.
c) A function that calls itself recursively.
d) A function bound with bind().
Correct Answer: a) A function passed as an argument to another function to be executed later.
Explanation:
Callback functions are passed into higher-order functions to be invoked at a designated time during execution.

58. What will be printed to the console? function calc(base, fn) { return fn(base); } console.log(calc(5, x => x * 2));

a) 10
b) 5
c) undefined
d) NaN
Correct Answer: a) 10
Explanation:
The inline arrow callback (x => x * 2) receives base (5) and returns 5 * 2 = 10.

59. What is callback hell in asynchronous JavaScript programming?

a) Nested callbacks forming deep pyramids of code that are difficult to read and maintain.
b) An infinite loop created by recursive function calls.
c) Passing null as a callback function.
d) An unhandled exception in a try-catch block.
Correct Answer: a) Nested callbacks forming deep pyramids of code that are difficult to read and maintain.
Explanation:
Callback hell describes deeply nested callback chains that hinder code readability and exception handling.

60. Which modern JavaScript features were introduced to mitigate callback hell?

a) Promises and async/await
b) Generators and yield
c) Arrow functions and default parameters
d) Rest and spread syntax
Correct Answer: a) Promises and async/await
Explanation:
Promises and async/await offer clean, linear control flow syntax to manage asynchronous operations.

61. What will be printed to the console? const fn = () => { return { a: 1 }; }; console.log(fn());

a) { a: 1 }
b) undefined
c) SyntaxError
d) null
Correct Answer: a) { a: 1 }
Explanation:
Using explicit return statements inside arrow function curly braces correctly returns the object { a: 1 }.

62. What will be printed to the console? const fn = () => { a: 1 }; console.log(fn());

a) undefined
b) { a: 1 }
c) SyntaxError
d) 1
Correct Answer: a) undefined
Explanation:
Curly braces without parentheses are parsed as a function block, interpreting 'a:' as a statement label and returning undefined.

63. How can an arrow function implicitly return an object literal without an explicit return statement?

a) const fn = () => ({ a: 1 });
b) const fn = () => { a: 1 };
c) const fn = () => return { a: 1 };
d) const fn = () => [ a: 1 ];
Correct Answer: a) const fn = () => ({ a: 1 });
Explanation:
Wrapping an object literal in parentheses `({ ... })` tells JavaScript to evaluate the expression as an implicit object return.

64. What will be printed to the console? let x = 1; function test(x = 2, y = function() { x = 3; }) { y(); console.log(x); } test();

a) 3
b) 2
c) 1
d) undefined
Correct Answer: a) 3
Explanation:
Function y modifies the local parameter variable x (initialized to 2), updating it to 3 within the parameter scope.

65. What is a methodology called where a function is executed automatically with a new context for setup?

a) IIFE
b) Factory pattern
c) Currying
d) Memoization
Correct Answer: a) IIFE
Explanation:
IIFEs (Immediately Invoked Function Expressions) run setup logic instantly while keeping variables isolated.

66. What will be printed to the console? function print() { console.log(this.val); } const obj1 = { val: 10, print }; const obj2 = { val: 20, print }; obj1.print.call(obj2);

a) 20
b) 10
c) undefined
d) TypeError
Correct Answer: a) 20
Explanation:
Explicit binding via .call(obj2) overrides object method invocation, setting 'this' to obj2.

67. What happens if you use Function.prototype.bind() twice on the same function?

a) The second bind() replaces the first target context.
b) The bound function retains the context established by the first bind() call.
c) It throws a TypeError.
d) It resets 'this' to global scope.
Correct Answer: b) The bound function retains the context established by the first bind() call.
Explanation:
Hard binding established by the first .bind() call cannot be overridden by subsequent .bind() calls.

68. What will be printed to the console? function foo() { console.log(this.a); } const boundFoo = foo.bind({ a: 1 }); boundFoo.call({ a: 2 });

a) 1
b) 2
c) undefined
d) TypeError
Correct Answer: a) 1
Explanation:
Bound functions permanently retain their initial bound 'this' target ({ a: 1 }), ignoring later .call() re-bindings.

69. What is the result of evaluating: `(function() { return typeof arguments; })()`?

a) "object"
b) "array"
c) "arguments"
d) "undefined"
Correct Answer: a) "object"
Explanation:
In JavaScript, the internal type of the arguments object returns "object".

70. Which of the following describes method borrowing in JavaScript?

a) Using call() or apply() to run a method of one object on another object.
b) Inheriting methods through class extends.
c) Copying function strings using toString().
d) Importing functions from external ES modules.
Correct Answer: a) Using call() or apply() to run a method of one object on another object.
Explanation:
Method borrowing allows an object to reuse another object's function by setting 'this' via call or apply.

71. What will be logged by this code? const obj = { 0: "a", 1: "b", length: 2 }; const result = Array.prototype.slice.call(obj); console.log(Array.isArray(result));

a) true
b) false
c) undefined
d) TypeError
Correct Answer: a) true
Explanation:
Borrowing Array.prototype.slice converts array-like objects (having indices and length) into true Array instances.

72. What is a factory function in JavaScript?

a) A function that creates and returns a new object without using the 'new' keyword.
b) A constructor function used with 'new'.
c) A generator function yielding object instances.
d) A function defined inside a class.
Correct Answer: a) A function that creates and returns a new object without using the 'new' keyword.
Explanation:
Factory functions are regular functions that construct and return object instances directly.

73. What is the output of the following code? function createUser(name) { return { getName() { return name; } }; } const u = createUser("Eve"); console.log(u.getName());

a) Eve
b) undefined
c) TypeError
d) null
Correct Answer: a) Eve
Explanation:
The returned object method forms a closure encapsulating the 'name' parameter ("Eve").

74. Can private variables be achieved using closures in factory functions?

a) Yes, local variables defined inside the factory function are accessible only by enclosed methods.
b) No, all properties in JavaScript objects are strictly public.
c) Only if strict mode is enabled.
d) Only when using class declarations.
Correct Answer: a) Yes, local variables defined inside the factory function are accessible only by enclosed methods.
Explanation:
Factory functions leverage function scope and closures to keep internal state variables private.

75. What is the result of invoking a generator function `function* gen() {}` directly without `new`?

a) Returns a Generator object conforming to Iterator and Iterable protocols.
b) Executes the body immediately to completion.
c) Returns undefined.
d) Throws a TypeError.
Correct Answer: a) Returns a Generator object conforming to Iterator and Iterable protocols.
Explanation:
Invoking a generator function creates and returns an unstarted Generator iterator object.

76. What will be printed to the console? function* numbers() { yield 1; return 2; yield 3; } const g = numbers(); console.log(g.next()); console.log(g.next()); console.log(g.next());

a) {value: 1, done: false}, {value: 2, done: true}, {value: undefined, done: true}
b) {value: 1, done: false}, {value: 2, done: false}, {value: 3, done: false}
c) {value: 1, done: false}, {value: 3, done: false}, {value: undefined, done: true}
d) TypeError
Correct Answer: a) {value: 1, done: false}, {value: 2, done: true}, {value: undefined, done: true}
Explanation:
A 'return' statement inside a generator finishes iteration early, returning { value: 2, done: true }. Subsequent yields are ignored.

77. What will be printed to the console? function outer() { var a = 1; function inner() { var a = 2; console.log(a); } inner(); } outer();

a) 2
b) 1
c) undefined
d) ReferenceError
Correct Answer: a) 2
Explanation:
Variable 'a' declared inside inner() shadows variable 'a' in outer(), logging 2.

78. What will be printed to the console? function outer() { var a = 1; function inner() { console.log(a); } var a = 2; inner(); } outer();

a) 2
b) 1
c) undefined
d) ReferenceError
Correct Answer: a) 2
Explanation:
The inner function holds a reference to variable 'a'. When inner() runs, 'a' has already been reassigned to 2.

79. What is function decomposition?

a) Breaking a complex function into smaller, manageable, and reusable pure functions.
b) Deleting a function definition at runtime.
c) Converting async functions to synchronous functions.
d) Compiling JavaScript code into machine bytecode.
Correct Answer: a) Breaking a complex function into smaller, manageable, and reusable pure functions.
Explanation:
Decomposition reduces complexity by splitting monolithic tasks into smaller single-purpose functions.

80. What will be printed to the console? function fn(a, b = 2, c) { console.log(a, b, c); } fn(1, undefined, 3);

a) 1 2 3
b) 1 undefined 3
c) 1 2 undefined
d) NaN
Correct Answer: a) 1 2 3
Explanation:
Passing 'undefined' for 'b' activates its default value (2), so arguments print as 1 2 3.

81. What will be printed to the console? function fn(a, b = 2, c) { console.log(a, b, c); } fn(1, null, 3);

a) 1 null 3
b) 1 2 3
c) 1 undefined 3
d) TypeError
Correct Answer: a) 1 null 3
Explanation:
Default parameter values are triggered ONLY by 'undefined'. Passing 'null' explicitly assigns null to b.

82. What is function composition in JavaScript?

a) Combining two or more functions to produce a new function (e.g., f(g(x))).
b) Inheriting properties across prototypes.
c) Executing multiple functions concurrently in parallel threads.
d) Defining methods inside object literals.
Correct Answer: a) Combining two or more functions to produce a new function (e.g., f(g(x))).
Explanation:
Function composition passes the output of one function as the input argument to another function.

83. What is the output of this simple function composition? const double = x => x * 2; const square = x => x * x; const compose = (f, g) => x => f(g(x)); console.log(compose(square, double)(3));

a) 36
b) 18
c) 12
d) 81
Correct Answer: a) 36
Explanation:
double(3) evaluates to 6. Then square(6) evaluates to 36.

84. What is the output of reversing the composition order? const double = x => x * 2; const square = x => x * x; const compose = (f, g) => x => f(g(x)); console.log(compose(double, square)(3));

a) 18
b) 36
c) 12
d) 81
Correct Answer: a) 18
Explanation:
square(3) evaluates to 9. Then double(9) evaluates to 18.

85. What will be printed to the console? function fn() { return Array.from(arguments).reduce((a, b) => a + b, 0); } console.log(fn(10, 20, 30));

a) 60
b) 10
c) NaN
d) TypeError
Correct Answer: a) 60
Explanation:
Array.from converts 'arguments' to a true array, allowing reduce to calculate 10 + 20 + 30 = 60.

86. What will be printed to the console? const fn = (...args) => args.reduce((a, b) => a + b, 0); console.log(fn(10, 20, 30));

a) 60
b) 10
c) NaN
d) TypeError
Correct Answer: a) 60
Explanation:
The rest parameter syntax collects parameters into a true Array instance directly, summing to 60.

87. Which statement about arrow functions is FALSE?

a) They do not have their own 'this' binding.
b) They do not have their own 'arguments' object.
c) They can be used as constructor functions with 'new'.
d) They cannot be used as generator functions (no yield).
Correct Answer: c) They can be used as constructor functions with 'new'.
Explanation:
Arrow functions lack construct capabilities and throw a TypeError if called with 'new'.

88. What is the output of the following function invocation? function test() { return function() { return "Inner"; }; } console.log(typeof test());

a) "function"
b) "string"
c) "object"
d) "undefined"
Correct Answer: a) "function"
Explanation:
test() returns the un-invoked inner function definition, so typeof yields "function".

89. What is the output of invoking the inner function directly? function test() { return function() { return "Inner"; }; } console.log(test()());

a) Inner
b) undefined
c) TypeError
d) function
Correct Answer: a) Inner
Explanation:
test() returns the inner function, and the trailing `()` invokes it immediately, returning "Inner".

90. What will be logged by the code snippet below? let count = 0; function inc() { count++; } inc(); inc(); console.log(count);

a) 2
b) 1
c) 0
d) undefined
Correct Answer: a) 2
Explanation:
inc() modifies the global variable 'count' twice through side effects, setting count to 2.

91. Why are side effects discouraged in pure functional programming?

a) They make code state unpredictable, harder to test, and introduce unexpected bugs.
b) They slow down network execution.
c) They cause parse-time SyntaxErrors.
d) They prevent variable hoisting.
Correct Answer: a) They make code state unpredictable, harder to test, and introduce unexpected bugs.
Explanation:
Avoiding side effects guarantees predictable function outputs and simplifies testing.

92. What is the output of the following script? const obj = { a: 1, b: function() { return () => this.a; } }; const fn = obj.b(); console.log(fn());

a) 1
b) undefined
c) TypeError
d) null
Correct Answer: a) 1
Explanation:
obj.b() binds 'this' to obj during execution. The returned arrow function lexically retains this context, yielding 1.

93. What will be logged if obj.b is assigned to a detached variable first? const obj = { a: 1, b: function() { return () => this.a; } }; const detachedB = obj.b; const fn = detachedB(); console.log(fn());

a) undefined
b) 1
c) TypeError
d) ReferenceError
Correct Answer: a) undefined
Explanation:
detachedB() is invoked without an explicit object receiver, causing 'this' inside b to evaluate to global/undefined, so this.a is undefined.

94. What will be printed to the console? function foo(a, b = a * 2) { console.log(b); } foo(3);

a) 6
b) 3
c) NaN
d) undefined
Correct Answer: a) 6
Explanation:
Parameter b evaluates expression a * 2 (3 * 2), outputting 6.

95. What will be printed to the console? function foo(a, b = a * 2) { console.log(b); } foo(3, 10);

a) 10
b) 6
c) 3
d) NaN
Correct Answer: a) 10
Explanation:
Providing an explicit argument (10) overrides the default parameter expression.

96. Which array iteration method executes a provided callback function once for each element without returning a new array?

a) map()
b) forEach()
c) filter()
d) reduce()
Correct Answer: b) forEach()
Explanation:
forEach runs side effects for each element in an array and always returns undefined.

97. Which higher-order array method returns a new array containing elements that pass a conditional test function?

a) filter()
b) map()
c) some()
d) every()
Correct Answer: a) filter()
Explanation:
filter() creates a new array populated with elements that evaluate to truthy in the callback test.

98. Which higher-order array method transforms each array element using a callback function and returns a new array?

a) map()
b) filter()
c) forEach()
d) reduce()
Correct Answer: a) map()
Explanation:
map() transforms every element of an array based on the callback's return value and produces a new array of identical length.

99. What will be logged by this code? const nums = [1, 2, 3]; const result = nums.map(x => x * 2); console.log(result);

a) [2, 4, 6]
b) [1, 2, 3]
c) 6
d) undefined
Correct Answer: a) [2, 4, 6]
Explanation:
map multiplies each array element by 2, returning a new array [2, 4, 6].

100. What will be logged by this code? const nums = [1, 2, 3, 4]; const result = nums.filter(x => x % 2 === 0); console.log(result);

a) [2, 4]
b) [1, 3]
c) true
d) [2]
Correct Answer: a) [2, 4]
Explanation:
filter retains elements that satisfy even condition `x % 2 === 0`, yielding array [2, 4].

101. What is the output of the following function statement? function test() { return console.log("Hello"); } console.log(test());

a) Hello followed by undefined
b) Hello followed by Hello
c) undefined followed by Hello
d) Hello
Correct Answer: a) Hello followed by undefined
Explanation:
console.log("Hello") prints "Hello" and returns undefined. test() returns that undefined value, which is then logged.

102. What is the primary function of the `Function` constructor `new Function('a', 'b', 'return a + b')`?

a) Creates a function dynamically from string arguments.
b) Converts string code into JSON.
c) Executes functions in web workers.
d) Binds functions to HTML forms.
Correct Answer: a) Creates a function dynamically from string arguments.
Explanation:
The Function constructor compiles functions dynamically from strings representing parameter names and function body code.

103. What is the evaluation result of: `(function(a) { delete a; return a; })(1)`?

a) 1
b) undefined
c) TypeError
d) ReferenceError
Correct Answer: a) 1
Explanation:
The delete operator removes properties from objects. It cannot delete local function parameters or variables, so 'a' remains 1.
← Previous: JavaScript Event Handling MCQs
Next →: JavaScript Hoisting MCQs
Latest Python Dictionaries MCQs

Latest Python Dictionaries MCQs

Python dictionaries are built-in data structures used to store data in key-value pairs, providing fast lookups, insertions, and deletions. Keys…

By MCQs Generator
Java Functions MCQs

Java Functions MCQs

In Java, functions are formally known as methods and serve as modular blocks of reusable code encapsulated within classes. A…

By MCQs Generator
C++ Memory Management MCQs

C++ Memory Management MCQs

Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…

By MCQs Generator