JavaScript MCQs - Closures, Scope and Hoisting (Set 1)

JavaScript MCQs - Closures, Scope and Hoisting (Set 1) - CodingHumans

These are the JavaScript basics interviewers check first: how hoisting really works, var vs let vs const, the temporal dead zone, lexical scope and closures. Most questions ask for the output, so read each snippet slowly before you answer.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 16 questions.

JavaScript MCQ series: Set 1 | Set 2 | Set 3 | Set 4

Topics covered in this set

  • Hoisting of var, let, const, functions and classes
  • Temporal dead zone (TDZ)
  • Block scope vs function scope
  • Lexical scope and shadowing
  • Closures and private state
  • IIFE and the global object

JavaScript MCQs with answers and explanations

Q1. What is printed?

console.log(a);
var a = 5;

a) null

b) ReferenceError

c) undefined

d) 5

Q2. What happens when this code runs?

console.log(b);
let b = 5;

a) Prints null

b) Throws ReferenceError

c) Prints undefined

d) Prints 5

Q3. What is printed?

sayHi();
function sayHi() {
  console.log('hi');
}

a) TypeError

b) ReferenceError

c) undefined

d) hi

Q4. What happens when this code runs?

greet();
var greet = function () {
  console.log('hello');
};

a) Prints undefined

b) TypeError: greet is not a function

c) ReferenceError

d) Prints hello

Q5. Which statement best describes a closure in JavaScript?

a) A function together with references to the variables of the scope where it was created

b) A function that cannot access global variables

c) A function that is executed immediately

d) An object that hides its properties using Symbols

Q6. What is printed?

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

a) 2

b) 0

c) 3

d) 1

Q7. What is printed?

{
  let x = 1;
  var y = 2;
}
console.log(typeof x, y);

a) undefined 2

b) undefined undefined

c) ReferenceError

d) number 2

Q8. What happens when f() is called?

let x = 1;
function f() {
  console.log(x);
  let x = 2;
}
f();

a) Throws ReferenceError

b) Prints 2

c) Prints 1

d) Prints undefined

Q9. What is the result?

const user = { name: 'Asha' };
user.name = 'Ravi';
console.log(user.name);

a) Asha

b) Ravi

c) TypeError: Assignment to constant variable

d) undefined

Q10. What is printed?

let n = 'global';
function show() {
  console.log(n);
}
function run() {
  let n = 'local';
  show();
}
run();

a) undefined

b) global

c) local

d) ReferenceError

Q11. What is the main purpose of an IIFE such as (function () { ... })();?

a) To make a function run faster

b) To hoist the function to the top of the file

c) To create a private scope so variables do not leak into the global scope

d) To convert a function into a class

Q12. What is printed?

var foo = 1;
function foo() {}
console.log(typeof foo);

a) undefined

b) function

c) object

d) number

Q13. Which statement about closures and memory is correct?

a) Closures are garbage collected immediately after the outer function returns

b) Closures copy outer variables, so changes outside are never seen

c) Only global variables can be captured

d) Outer variables captured by a closure stay in memory as long as the closure is reachable

Q14. What is printed?

function once(fn) {
  let done = false, result;
  return (...args) => {
    if (!done) {
      done = true;
      result = fn(...args);
    }
    return result;
  };
}
let calls = 0;
const init = once(() => ++calls);
init();
init();
console.log(calls);

a) 0

b) undefined

c) 1

d) 2

Q15. What happens when this code runs?

const p = new Person();
class Person {}

a) Throws TypeError

b) Creates a Person instance

c) p is undefined

d) Throws ReferenceError

Q16. In a classic (non-module) browser script, which top-level declaration creates a property on the window object?

a) var count = 0

b) let count = 0

c) class Count {}

d) const count = 0

Quick revision notes

  • var is function scoped and hoisted with the value undefined.
  • let, const and class are hoisted but stay in the temporal dead zone until their declaration runs.
  • Function declarations are hoisted with their body. Function expressions are not.
  • Scope is lexical. It depends on where the code is written, not where it is called.
  • A closure keeps a live link to outer variables. That gives you private state, but it can also hold memory longer than you expect.

Frequently asked questions

Are let and const hoisted in JavaScript?

Yes. They are hoisted but not given a value. If you use them before their line, you get a ReferenceError, because they are still in the temporal dead zone.

What is a real-world use of closures?

You use them all the time: private state (counters, once, memoize), event handlers that remember data, debounce and throttle helpers, and React hooks, which depend on closures over component state.

What is the temporal dead zone?

It is the gap between entering a scope and reaching the line where a let, const or class is declared. During that gap the name exists, but you can't read or write it.

Comments

Answered 0/0Correct 0