Instructions

  • 1. Your final score will reflect your grasp of the concepts—approach each question with precision.
  • 2. Thoroughly review each solution before proceeding to ensure full understanding.
  • 3. Final results will be available after submission to provide insights into areas for further improvement.
  • 4. Maintain academic integrity—plagiarism undermines learning and professional growth.
  • 5. Once submitted, responses are final, so ensure you’re confident in your answers.
  • 6. These challenges are designed to test practical knowledge; apply your skills as you would in real-world scenarios.

All Problems

Question

Action

What is a closure in JavaScript?

View

Which of the following code snippets demonstrates a closure?

View

Closures in JavaScript help in creating which of the following?

View

In which situation would a closure be useful?

View

What will the following code output?

View

Which of the following best describes how closures handle variable scope?

View

What happens to variables defined in the outer function when a closure is created?

View

In which scenario is it difficult to avoid memory leaks with closures?

View

How can closures be used to create a "private method"?

View

What will the following code output?

View

What is a closure in JavaScript?

A function bundled with its lexical environment
A global variable accessible by all functions
A function that returns another function
A function that prevents the use of this

Which of the following code snippets demonstrates a closure?

function outer() { let count = 0; return function inner() { count++; return count; }; }

outer() creates a closure
inner() creates a closure
Both outer() and inner() create closures
No closure is created

Closures in JavaScript help in creating which of the following?

Private variables
Asynchronous functions
Object prototypes
Dynamic classes

In which situation would a closure be useful?

When working with global variables
When you need to preserve local state between function calls
When avoiding prototype-based inheritance
When working with arrays

What will the following code output?

function makeCounter() { let count = 0; return function() { count++; console.log(count); }; } const counter1 = makeCounter(); counter1(); counter1();

1, 2
0, 1
2, 3
1, 1

Which of the following best describes how closures handle variable scope?

They store a reference to the local variable environment
They create a copy of all variables in the outer function
They discard the local scope when the outer function returns
They limit access to only global variables

What happens to variables defined in the outer function when a closure is created?

They are permanently stored in memory
They are garbage collected once the outer function finishes
They remain accessible to the inner function
They are reset every time the closure is invoked

In which scenario is it difficult to avoid memory leaks with closures?

When closures refer to large objects
When closures are used inside event listeners
When closures are returned from anonymous functions
When closures are used in for loops

How can closures be used to create a "private method"?

By using the this keyword to reference the method
By returning a function that references variables from the outer function
By defining the method inside a constructor function
By using Object.freeze() on the method

What will the following code output?

let globalVar = 5; function outerFunc() { let localVar = 10; return function() { return localVar + globalVar; }; } const innerFunc = outerFunc(); console.log(innerFunc());

15
10
5
20