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 lexical environment in JavaScript?

View

What is the key feature of a closure?

View

What will the following code output? function outer() { let x = 5; return function inner() { return x + 2; }; } const result = outer(); console.log(result());

View

Can closures access global variables?

View

What will this code print? function counter() { let count = 0; return function() { count++; return count; }; } const increment = counter(); console.log(increment()); console.log(increment());

View

Why do closures maintain access to outer variables after the outer function finishes execution?

View

What will this code log? let name = "Alice"; function greet() { console.log(name); } greet();

View

What happens if a closure function is executed multiple times?

View

What is one common use of closures in JavaScript?

View

What does the following code print? function outer() { let secret = "hidden"; return function() { return secret; }; } let reveal = outer(); console.log(reveal());

View

What is a lexical environment in JavaScript?

A runtime environment for synchronous code
A way to define variables globally
The environment in which variables and functions are declared
An internal JavaScript object

What is the key feature of a closure?

It prevents access to outer variables
It has access to its own scope and the outer scope
It returns only strings
It doesn't take parameters

What will the following code output? function outer() { let x = 5; return function inner() { return x + 2; }; } const result = outer(); console.log(result());

2
5
7
Undefined

Can closures access global variables?

Yes
No
Only in strict mode
Only within the same file

What will this code print? function counter() { let count = 0; return function() { count++; return count; }; } const increment = counter(); console.log(increment()); console.log(increment());

1, 2
0, 1
2, 2
Undefined

Why do closures maintain access to outer variables after the outer function finishes execution?

Because of variable hoisting
Due to JavaScript's asynchronous nature
They preserve the lexical environment of the outer function
Because closures bind to the global scope

What will this code log? let name = "Alice"; function greet() { console.log(name); } greet();

Alice
undefined
Error
null

What happens if a closure function is executed multiple times?

It will forget its outer variable values
It will recreate the outer function's scope
It will maintain the values of the outer variables
It will throw an error

What is one common use of closures in JavaScript?

Implementing loops
Creating private variables and functions
Performing arithmetic operations
Handling network requests

What does the following code print? function outer() { let secret = "hidden"; return function() { return secret; }; } let reveal = outer(); console.log(reveal());

hidden
undefined
Error
null