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 key advantage of using closures for callback functions?

View

Which of the following is a common issue when using closures inside loops?

View

How can you fix the above issue with closures in loops?

View

What will the following code output?

View

How can closures help in managing memory leaks?

View

What is the purpose of closures in asynchronous operations like setTimeout?

View

What will the following code output?

View

In what scenario would a closure be likely to cause a memory leak?

View

How do closures interact with JavaScript's event loop and asynchronous operations?

View

What is a closure in JavaScript often used for?

View

What is a key advantage of using closures for callback functions?

They can execute functions in parallel
They remember the lexical scope in which they were defined
They are asynchronous by default
They always run immediately

Which of the following is a common issue when using closures inside loops?

<p>for (var i = 0; i &lt; 5; i++) {<br>&nbsp; setTimeout(function() {<br>&nbsp; &nbsp; console.log(i);<br>&nbsp; }, 1000);<br>}<br><br></p>

It will log 0, 1, 2, 3, 4
It will log 5, 5, 5, 5, 5
It will log an error
It will log undefined

How can you fix the above issue with closures in loops?

Use let instead of var
Use const instead of var
Wrap the setTimeout in an IIFE (Immediately Invoked Function Expression)
Use bind on the function

What will the following code output?

<p>function outer() {<br>&nbsp; let count = 0;<br>&nbsp; return function() {<br>&nbsp; &nbsp; return ++count;<br>&nbsp; };<br>}<br>const counter = outer();<br>console.log(counter());<br>console.log(counter());<br><br></p>

1, 1
0, 1
1, 2
Error

How can closures help in managing memory leaks?

By keeping variables in memory permanently
By freeing memory when the outer function is executed
By controlling the lifetime of variables
By making variables global

What is the purpose of closures in asynchronous operations like setTimeout?

They help maintain state and scope in callbacks
They immediately execute the callback
They block the event loop
They are not used in asynchronous operations

What will the following code output?

<p>function greet(name) {<br>&nbsp; return function() {<br>&nbsp; &nbsp; console.log("Hello, " + name);<br>&nbsp; };<br>}<br>const sayHello = greet("Alice");<br>sayHello();<br><br></p>

Hello, Alice"
undefined
"Hello, undefined"
Error

In what scenario would a closure be likely to cause a memory leak?

When closures reference large objects no longer needed
When closures are created inside arrow functions
When closures reference only primitive values
When closures reference only primitive values

How do closures interact with JavaScript's event loop and asynchronous operations?

Closures block the event loop
Closures execute once the event loop is finished
Closures maintain scope for asynchronous callbacks
Closures execute synchronously in async code

What is a closure in JavaScript often used for?

Avoiding inheritance issues
Encapsulating functions to access outer variables
Defining global variables
Creating object properties dynamically