LearnFrenzy
Sign in Take a free test

Closures · Exercise 1 of 4

Closures Closures MCQ

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Fifteen multiple-choice questions on JavaScript closures: the definition, environments per call, capturing variables, the loop problem, private state, the module pattern, memoization, stale closures, memory and this. Pick an option to check your answer instantly.

Question 1 Easy

Which statement best describes a closure?

A A function that closes the browser window
B A function that cannot be called again after it returns
C A function declared inside a class
D A function that keeps access to the variables of the scope where it was created, even after that scope's function has returned
Question page & discussion
Question 2 Medium

When does JavaScript create a closure?

A Whenever a function is created – it records the environment around it
B Only when a function is returned from another function
C Only when the closure keyword is used
D Only for arrow functions
Question page & discussion
Question 3 Medium

let n = 1; const f = () => n; n = 5; What does f() return?

A 1 – the value when f was created
B undefined
C 5 – the closure reads the variable's current value
D A ReferenceError
Question page & discussion
Question 4 Easy

const c1 = makeCounter(); const c2 = makeCounter(); where makeCounter keeps let count = 0 inside. Do c1 and c2 share count?

A Yes – count is shared by all counters
B No – each call creates its own count
C Only if makeCounter is an arrow function
D Only after the first call
Question page & discussion
Question 5 Medium

A function returns { inc: () => ++count, get: () => count } from one call. What do inc and get share?

A The same count variable, so get sees what inc changed
B Separate copies of count
C Nothing – arrow functions cannot share variables
D Only the initial value of count
Question page & discussion
Question 6 Easy

Callbacks created in for (var i = 0; i < 3; i++) all print 3. What is the simplest fix?

A Use setTimeout with a delay of 0
B Declare the loop variable with let
C Use an arrow function instead of a regular function
D Declare i with const outside the loop
Question page & discussion
Question 7 Easy

In function createWallet() { let balance = 0; return { add: (n) => (balance += n) }; }, how can outside code change balance?

A Only by calling add – balance itself is not reachable
B wallet.balance = 100
C createWallet.balance = 100
D window.balance = 100
Question page & discussion
Question 8 Hard

Which statement about closures and memory is true?

A Closures always cause memory leaks
B Captured variables are freed as soon as the outer function returns
C Variables a closure uses stay in memory as long as the closure itself is reachable
D Closures copy all global variables
Question page & discussion
Question 9 Medium

What makes the module pattern const store = (function () { let items = []; return { add, list }; })(); work?

A IIFEs create global variables
B items is copied into each method
C The returned methods close over items, which lives in the IIFE's scope
D The return statement freezes the object
Question page & discussion
Question 10 Hard

A click handler closes over a large array. When can that array be garbage-collected?

A Immediately after the outer function returns
B Never, while the page is open
C After the handler is removed and nothing else references it or the array
D After the first click
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links