MCQs on Advanced Functions and Scope | JavaScript Intermediate

Dive deep into closures, lexical scope, higher-order functions, callbacks, and IIFE to enhance your JavaScript skills and write optimized, maintainable, and powerful code.


Closures and Lexical Scope (8 MCQs)

  1. What is a closure in JavaScript?
    • A) A function bundled with its lexical environment
    • B) A method to close an open file
    • C) A block of code executed immediately
    • D) A syntax for declaring variables
  2. Which of the following is a key feature of closures?
    • A) They can modify variables declared in their outer scope
    • B) They do not allow variable sharing
    • C) They execute outside their lexical environment
    • D) They can only be called once
  3. What determines the lexical scope of a function in JavaScript?
    • A) The function’s arguments
    • B) Where the function is defined in the code
    • C) The function’s name
    • D) The browser’s JavaScript engine
  4. How can closures be used in JavaScript?
    • A) To prevent memory leaks
    • B) To create private variables
    • C) To execute code asynchronously
    • D) To modify the global object
  5. Which of the following scenarios demonstrates a closure?
    • A) A function that calls another function inside its body
    • B) A function that returns another function and uses variables from the outer scope
    • C) A function that has no parameters
    • D) A function that calls itself recursively
  6. What happens to the variables used in a closure after the outer function has executed?
    • A) They are deleted immediately
    • B) They remain accessible through the closure
    • C) They are moved to the global scope
    • D) They are inaccessible
  7. Which of the following best describes lexical scope?
    • A) Scope determined at runtime based on the call stack
    • B) Scope determined during the declaration phase
    • C) Scope that can be modified dynamically
    • D) Scope limited to local variables only
  8. What is the output of this code?javascriptCopy codefunction outer() { let count = 0; return function inner() { count++; return count; }; } const counter = outer(); console.log(counter()); // ? console.log(counter()); // ?
    • A) 1, 2
    • B) 0, 1
    • C) 2, 2
    • D) Error

Higher-Order Functions (8 MCQs)

  1. What is a higher-order function in JavaScript?
    • A) A function that returns an object
    • B) A function that accepts or returns another function
    • C) A function with nested loops
    • D) A function declared with the async keyword
  2. Which of the following is an example of a higher-order function?
  • A) A function that calculates the square root
  • B) Array.map()
  • C) Math.pow()
  • D) A function that declares a variable
  1. Why are higher-order functions important in JavaScript?
  • A) They enhance performance
  • B) They reduce code redundancy
  • C) They enable functional programming techniques
  • D) All of the above
  1. Which of the following functions is not considered a higher-order function?
  • A) Array.forEach()
  • B) Array.reduce()
  • C) console.log()
  • D) Array.filter()
  1. How do higher-order functions achieve reusability?
  • A) By creating multiple objects
  • B) By abstracting common logic into reusable components
  • C) By avoiding recursion
  • D) By using var instead of let
  1. What does the following code return?
javascriptCopy codeconst multiply = (a) => (b) => a * b;
const double = multiply(2);
console.log(double(5));
  • A) 7
  • B) 10
  • C) 25
  • D) 2
  1. How does Array.reduce() work as a higher-order function?
  • A) It combines all elements into a single value based on a callback function
  • B) It filters elements from an array
  • C) It modifies each element in place
  • D) It executes functions asynchronously
  1. What is the benefit of using Array.map() in conjunction with arrow functions?
  • A) It simplifies code readability
  • B) It prevents runtime errors
  • C) It reduces array length
  • D) It automatically filters out duplicates

Callback Functions (8 MCQs)

  1. What is a callback function in JavaScript?
  • A) A function that is executed immediately upon declaration
  • B) A function passed as an argument to another function
  • C) A function that runs in the global scope
  • D) A function without a return value
  1. Which of the following scenarios demonstrates the use of a callback function?
  • A) Logging a message after data is fetched from an API
  • B) Declaring a variable
  • C) Using Math.random()
  • D) Executing code inside a for loop
  1. What does the following code demonstrate?
javascriptCopy codefunction greet(name, callback) {
    console.log(`Hello, ${name}`);
    callback();
}
greet("Alice", () => console.log("Welcome!"));
  • A) Asynchronous programming
  • B) Callback functions
  • C) Higher-order functions
  • D) Promises
  1. How can callback functions lead to “callback hell”?
  • A) By reducing code readability when nested deeply
  • B) By using arrow functions incorrectly
  • C) By causing infinite loops
  • D) By breaking the call stack
  1. Which method uses callback functions for asynchronous operations?
  • A) fetch()
  • B) setTimeout()
  • C) Promise.then()
  • D) All of the above
  1. What is the output of the following code?
javascriptCopy codesetTimeout(() => console.log("First"), 0);
console.log("Second");
  • A) First, Second
  • B) Second, First
  • C) First only
  • D) Error
  1. How can callback functions be avoided in modern JavaScript?
  • A) By using IIFE
  • B) By using Promises or async/await
  • C) By using closures
  • D) By using synchronous code only
  1. What is the primary purpose of using a callback function?
  • A) To make code run faster
  • B) To handle asynchronous tasks
  • C) To avoid recursion
  • D) To modify global variables

IIFE (Immediately Invoked Function Expression) (6 MCQs)

  1. What is an IIFE in JavaScript?
  • A) A function invoked as soon as it is defined
  • B) A function that cannot be reused
  • C) A type of closure
  • D) A global function
  1. What is the main benefit of using an IIFE?
  • A) To define private variables
  • B) To increase runtime performance
  • C) To reduce memory usage
  • D) To modify the global object
  1. How is an IIFE written in JavaScript?
javascriptCopy code(function() {
    console.log("Hello!");
})();
  • A) Correct syntax
  • B) Incorrect syntax
  • C) Asynchronous declaration
  • D) Deprecated syntax
  1. Why are IIFEs often used in JavaScript modules?
  • A) To initialize code without polluting the global scope
  • B) To avoid using closures
  • C) To reduce callback hell
  • D) To reuse global variables
  1. Which of the following is a valid example of an IIFE?
  • A) function() {}();
  • B) (function() {})();
  • C) function() {}()();
  • D) (function() {});
  1. What is the scope of variables defined inside an IIFE?
  • A) Global scope
  • B) Local to the IIFE
  • C) Lexical scope of the parent function
  • D) Shared across all IIFEs

Answers

QnoAnswer
1A) A function bundled with its lexical environment
2A) They can modify variables declared in their outer scope
3B) Where the function is defined in the code
4B) To create private variables
5B) A function that returns another function and uses variables from the outer scope
6B) They remain accessible through the closure
7B) Scope determined during the declaration phase
8A) 1, 2
9B) A function that accepts or returns another function
10B) Array.map()
11D) All of the above
12C) console.log()
13B) By abstracting common logic into reusable components
14B) 10
15A) It combines all elements into a single value based on a callback function
16A) It simplifies code readability
17B) A function passed as an argument to another function
18A) Logging a message after data is fetched from an API
19B) Callback functions
20A) By reducing code readability when nested deeply
21D) All of the above
22B) Second, First
23B) By using Promises or async/await
24B) To handle asynchronous tasks
25A) A function invoked as soon as it is defined
26A) To define private variables
27A) Correct syntax
28A) To initialize code without polluting the global scope
29B) (function() {})();
30B) Local to the IIFE

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top