JavaScript Hoisting MCQs

1 min read

Hoisting is a fundamental mechanism in JavaScript where variable and function declarations are notionally moved to the top of their containing scope during the compilation phase, prior to code execution. This behavior is governed by the creation phase of execution contexts, where memory space is allocated for identifiers. While traditional var declarations are hoisted and initialized with undefined, modern ES6 block-scoped declarations (let and const) are hoisted uninitialized into the Temporal Dead Zone (TDZ). Furthermore, function declarations are fully hoisted with their bodies intact, whereas function expressions remain unhoisted. Mastering these execution mechanics is vital for predicting code output and clearing technical software engineering interviews.

1. What is hoisting in JavaScript?

a) A mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase before code execution
b) An automatic garbage collection routine that clears unused memory
c) A compiler optimization that compiles code into binary WebAssembly
d) A runtime error handling mechanism for asynchronous promises
Correct Answer: a) A mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase before code execution
Explanation:
Hoisting conceptually moves declarations to the top of their scope during compilation, allowing functions and variables to be referenced before they appear in the source code.

2. How are variables declared with the `var` keyword affected by hoisting?

a) They are hoisted to the top of their scope and automatically initialized with `undefined`.
b) They are hoisted and initialized with their assigned value immediately.
c) They are not hoisted at all and throw a ReferenceError.
d) They are hoisted into the global window object as frozen constants.
Correct Answer: a) They are hoisted to the top of their scope and automatically initialized with `undefined`.
Explanation:
Var declarations are hoisted and initialized with `undefined`, meaning accessing them before their declaration line returns `undefined` rather than throwing an error.

3. What happens when you access a `let` or `const` variable before its declaration line?

a) It throws a ReferenceError because it resides in the Temporal Dead Zone (TDZ).
b) It returns `undefined` just like `var` declarations.
c) It evaluates to `null` silently.
d) It returns the global window object property.
Correct Answer: a) It throws a ReferenceError because it resides in the Temporal Dead Zone (TDZ).
Explanation:
While `let` and `const` are hoisted, they are not initialized with a default value, creating a Temporal Dead Zone from the start of the block until the declaration is evaluated.

4. What is the Temporal Dead Zone (TDZ)?

a) The execution phase from the start of a block scope until a `let` or `const` variable is fully initialized, during which accessing it throws a ReferenceError
b) The time period when asynchronous timers wait in the message queue
c) The garbage collection pause window in the V8 engine
d) The memory state after a variable is deleted
Correct Answer: a) The execution phase from the start of a block scope until a `let` or `const` variable is fully initialized, during which accessing it throws a ReferenceError
Explanation:
The TDZ ensures developers do not accidentally use block-scoped variables before their intended initialization point.

5. How are standard function declarations affected by hoisting?

a) They are fully hoisted along with their complete function body definition, allowing them to be invoked before their written line in code.
b) They are hoisted as `undefined` variables.
c) They are not hoisted and throw a SyntaxError if called early.
d) They are hoisted only into the global scope.
Correct Answer: a) They are fully hoisted along with their complete function body definition, allowing them to be invoked before their written line in code.
Explanation:
Function declarations are completely hoisted with their bodies, making them executable anywhere within their enclosing scope.

6. How are function expressions assigned to variables (e.g., `const foo = function() {}`) handled during hoisting?

a) Only the variable identifier is hoisted according to its declaration keyword (`var`, `let`, or `const`), while the function assignment itself is not hoisted.
b) The entire function body and assignment are hoisted.
c) They are treated as function declarations and fully hoisted.
d) They cause an immediate parsing syntax exception.
Correct Answer: a) Only the variable identifier is hoisted according to its declaration keyword (`var`, `let`, or `const`), while the function assignment itself is not hoisted.
Explanation:
Function expressions follow standard variable hoisting rules depending on whether they use `var`, `let`, or `const`.

7. What is the output of executing `console.log(a); var a = 5;`?

a) `undefined`
b) `5`
c) `ReferenceError`
d) `SyntaxError`
Correct Answer: a) `undefined`
Explanation:
Due to hoisting, the `var a` declaration moves to the top and initializes with `undefined` before `console.log` executes.

8. What is the output of executing `console.log(a); let a = 5;`?

a) `ReferenceError`
b) `undefined`
c) `5`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
Because `let` variables are in the Temporal Dead Zone until their declaration line, accessing `a` early throws a ReferenceError.

9. Are ES6 class declarations hoisted?

a) Yes, they are hoisted to the top of their scope, but they remain uninitialized in the Temporal Dead Zone, throwing a ReferenceError if instantiated before declaration.
b) No, they are never hoisted under any circumstances.
c) Yes, they are fully initialized with default prototypes during hoisting.
d) Only when declared as anonymous classes.
Correct Answer: a) Yes, they are hoisted to the top of their scope, but they remain uninitialized in the Temporal Dead Zone, throwing a ReferenceError if instantiated before declaration.
Explanation:
Class declarations behave similarly to `let` and `const` regarding hoisting and TDZ restrictions.

10. What happens when a variable declaration and a function declaration share the exact same name within the same scope during hoisting?

a) The function declaration takes precedence and overwrites the variable declaration during hoisting, unless the variable is subsequently assigned a value.
b) The variable declaration overwrites the function.
c) An immediate SyntaxError is thrown due to duplicate identifiers.
d) Both are ignored by the JavaScript engine.
Correct Answer: a) The function declaration takes precedence and overwrites the variable declaration during hoisting, unless the variable is subsequently assigned a value.
Explanation:
Function declarations are processed first during hoisting, meaning a function declaration overrides a `var` declaration with the same name.

11. In what phase of code lifecycle does hoisting actually occur in JavaScript engines?

a) Creation / Compilation phase
b) Execution phase
c) Garbage collection phase
d) Rendering / Paint phase
Correct Answer: a) Creation / Compilation phase
Explanation:
JavaScript engines parse the code and set up memory allocations for variable and function declarations during the creation phase before executing line-by-line.

12. Does hoisting physically move lines of code in your source file?

a) No, code lines remain physically where you wrote them; hoisting is a mental model representing how the engine allocates memory for declarations during compilation.
b) Yes, the V8 compiler physically rewrites the text file on disk.
c) Yes, parser shifts lines to the top of the AST.
d) Only when using strict mode.
Correct Answer: a) No, code lines remain physically where you wrote them; hoisting is a mental model representing how the engine allocates memory for declarations during compilation.
Explanation:
Code is not physically rearranged; rather, declarations are registered in the variable environment record during compilation.

13. How does hoisting behave inside block statements (`if`, `while`, `{}`) for `var` declarations?

a) `var` declarations ignore block boundaries and are hoisted to the enclosing function or global scope.
b) They are block-scoped and remain trapped inside the curly braces.
c) They throw a ReferenceError outside the block.
d) They are not hoisted at all.
Correct Answer: a) `var` declarations ignore block boundaries and are hoisted to the enclosing function or global scope.
Explanation:
Because `var` is function-scoped rather than block-scoped, it punches through block boundaries during hoisting.

14. How does hoisting behave inside block statements for `let` and `const` declarations?

a) They are block-scoped, hoisting only to the top of their immediate enclosing block curly braces.
b) They leak into the global scope automatically.
c) They behave like `var` and ignore block boundaries.
d) They are hoisted to the global window object.
Correct Answer: a) They are block-scoped, hoisting only to the top of their immediate enclosing block curly braces.
Explanation:
Block scoping confines `let` and `const` hoisting strictly to their enclosing block bounds.

15. What is the output of `foo(); function foo() { console.log('hello'); }`?

a) `hello`
b) `undefined`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `hello`
Explanation:
Function declarations are fully hoisted with their bodies, allowing successful invocation before their written line.

16. What is the output of `foo(); var foo = function() { console.log('hello'); }`?

a) `TypeError: foo is not a function`
b) `hello`
c) `ReferenceError`
d) `undefined`
Correct Answer: a) `TypeError: foo is not a function`
Explanation:
During hoisting, `var foo` becomes `undefined`. Calling `foo()` evaluates to `undefined()`, which throws a TypeError because `undefined` is not callable.

17. What is the output of `foo(); const foo = function() { console.log('hello'); }`?

a) `ReferenceError`
b) `TypeError: foo is not a function`
c) `undefined`
d) `hello`
Correct Answer: a) `ReferenceError`
Explanation:
Because `foo` is declared with `const`, it resides in the TDZ until execution reaches the assignment line, throwing a ReferenceError when called early.

18. How do arrow functions assigned to variables behave during hoisting?

a) They follow the exact same hoisting rules as the variable declaration keyword (`var`, `let`, or `const`) used to store them.
b) They are always fully hoisted like regular function declarations.
c) They are never hoisted under any circumstance.
d) They automatically hoist into the global scope.
Correct Answer: a) They follow the exact same hoisting rules as the variable declaration keyword (`var`, `let`, or `const`) used to store them.
Explanation:
Arrow functions are function expressions; their hoisting behavior depends entirely on whether `var`, `let`, or `const` is used.

19. What happens when you redeclare a `var` variable within the same scope due to hoisting?

a) Duplicate `var` declarations are ignored, and the existing value is preserved.
b) It throws a SyntaxError immediately.
c) It resets the variable value to `undefined` automatically.
d) It deletes the variable from memory.
Correct Answer: a) Duplicate `var` declarations are ignored, and the existing value is preserved.
Explanation:
JavaScript permits multiple `var` declarations of the same name in the same scope without syntax errors.

20. What happens when you redeclare a `let` variable within the same scope?

a) It throws a SyntaxError during parsing.
b) It overwrites the previous value successfully.
c) It ignores the duplicate declaration.
d) It converts the variable into a constant.
Correct Answer: a) It throws a SyntaxError during parsing.
Explanation:
Redeclaring a `let` variable within the exact same scope is strictly forbidden and throws a SyntaxError.

21. Can parameters defined in a function signature be hoisted?

a) Function parameters are implicitly declared and initialized inside the function scope during creation, acting as local variables.
b) Parameters are not hoisted and throw a ReferenceError.
c) Parameters are hoisted into the global window object.
d) Parameters require explicit `var` declarations.
Correct Answer: a) Function parameters are implicitly declared and initialized inside the function scope during creation, acting as local variables.
Explanation:
Parameters are scoped to the function body and initialized when the function is invoked.

22. What is the output of `var x = 10; { console.log(x); var x = 20; }`?

a) `undefined`
b) `10`
c) `20`
d) `ReferenceError`
Correct Answer: a) `undefined`
Explanation:
Because `var` is function-scoped, the inner `var x` hoists to the top of the enclosing function/global scope, shadowing outer `x` and initializing as `undefined` before `console.log`.

23. What is the output of `let x = 10; { console.log(x); let x = 20; }`?

a) `ReferenceError`
b) `10`
c) `20`
d) `undefined`
Correct Answer: a) `ReferenceError`
Explanation:
The inner block declares `let x`, creating a TDZ for that block scope. Accessing `x` before its declaration throws a ReferenceError instead of reading outer `x = 10`.

24. Does strict mode (`'use strict'`) alter hoisting rules for `var`, `let`, or `const`?

a) No, strict mode does not change basic hoisting mechanics, though it enforces stricter error checking on undeclared assignments.
b) Yes, strict mode disables hoisting entirely.
c) Yes, strict mode converts all `var` hoisting into block scope.
d) Yes, strict mode removes the Temporal Dead Zone.
Correct Answer: a) No, strict mode does not change basic hoisting mechanics, though it enforces stricter error checking on undeclared assignments.
Explanation:
Hoisting is a core compilation mechanism unaffected by the strict mode directive.

25. What is the behavior of hoisting when variables are assigned values without any declaration keyword (e.g., `x = 5;`)?

a) They are not hoisted because they are not declarations; they only become global properties when execution reaches that line.
b) They are hoisted as `undefined` variables.
c) They throw a ReferenceError during compilation.
d) They hoist into the nearest block scope.
Correct Answer: a) They are not hoisted because they are not declarations; they only become global properties when execution reaches that line.
Explanation:
Hoisting applies exclusively to declarations (`var`, `let`, `const`, `function`, `class`), not naked assignments.

26. What is the role of the Lexical Environment in managing hoisting during execution context creation?

a) It maintains Environment Records that store identifier bindings created during hoisting phases.
b) It compiles JavaScript source code into bytecode.
c) It manages HTML DOM event listener queues.
d) It handles garbage collection cycles.
Correct Answer: a) It maintains Environment Records that store identifier bindings created during hoisting phases.
Explanation:
Environment records map variable and function declarations during the creation phase of an execution context.

27. What happens to function expressions assigned inside conditional blocks (e.g., `if (true) { var f = function() {} }`) during hoisting?

a) The variable `f` is hoisted as `undefined` to the top of the function scope, but the function assignment occurs conditionally at runtime.
b) The entire function is hoisted conditionally.
c) An immediate SyntaxError is thrown.
d) The function is hoisted to the global scope.
Correct Answer: a) The variable `f` is hoisted as `undefined` to the top of the function scope, but the function assignment occurs conditionally at runtime.
Explanation:
Only the `var` declaration is hoisted; the assignment runs only when the conditional block executes.

28. How do generator functions behave during hoisting?

a) Generator function declarations are fully hoisted with their bodies, just like standard function declarations.
b) Generator functions are not hoisted and throw an error if called early.
c) Generator functions behave like `let` variables.
d) Generator functions hoist as `undefined`.
Correct Answer: a) Generator function declarations are fully hoisted with their bodies, just like standard function declarations.
Explanation:
Generator function declarations follow identical hoisting rules to standard functions.

29. What is the output of `console.log(typeof undeclaredVar);` when the variable has never been declared anywhere?

a) "undefined"
b) `ReferenceError`
c) "object"
d) "number"
Correct Answer: a) "undefined"
Explanation:
The `typeof` operator is safe to use on completely undeclared identifiers, returning "undefined" without throwing a ReferenceError.

30. What is the output of `console.log(typeof letVar); let letVar = 10;`?

a) `ReferenceError`
b) "undefined"
c) "let"
d) "number"
Correct Answer: a) `ReferenceError`
Explanation:
Unlike undeclared identifiers, evaluating `typeof` on a `let` or `const` variable inside its Temporal Dead Zone throws a ReferenceError.

31. Can an asynchronous `async function` declaration be hoisted?

a) Yes, async function declarations are fully hoisted along with their function bodies.
b) No, async functions are never hoisted.
c) Async functions hoist as `undefined`.
d) Async functions throw a SyntaxError during hoisting.
Correct Answer: a) Yes, async function declarations are fully hoisted along with their function bodies.
Explanation:
Async functions follow the exact same hoisting rules as standard function declarations.

32. What is the hoisting behavior of object destructuring assignments (e.g., `const {a} = obj;`)?

a) The declared variables follow the hoisting rules of their declaration keyword (`var`, `let`, or `const`), while the destructuring evaluation runs at execution time.
b) The entire object is hoisted during compilation.
c) Destructuring declarations are never hoisted.
d) They throw a SyntaxError during hoisting.
Correct Answer: a) The declared variables follow the hoisting rules of their declaration keyword (`var`, `let`, or `const`), while the destructuring evaluation runs at execution time.
Explanation:
Destructuring uses standard `var`, `let`, or `const` keywords, adhering to their respective hoisting and TDZ behaviors.

33. What is the hoisting behavior of array destructuring assignments?

a) The identifiers follow the standard hoisting rules of their respective declaration keywords (`var`, `let`, or `const`).
b) Array elements are hoisted into global scope.
c) Array destructuring bypasses hoisting entirely.
d) They cause a parsing exception.
Correct Answer: a) The identifiers follow the standard hoisting rules of their respective declaration keywords (`var`, `let`, or `const`).
Explanation:
Array destructuring relies on standard declaration keywords for hoisting behavior.

34. How do imported modules (`import` statements) interact with hoisting in ES6 modules?

a) Import declarations are hoisted to the top of the module file statically during compilation before any code execution.
b) Imports are evaluated lazily at runtime when reached.
c) Imports are not hoisted and throw a ReferenceError.
d) Imports behave like `var` declarations.
Correct Answer: a) Import declarations are hoisted to the top of the module file statically during compilation before any code execution.
Explanation:
ES6 import declarations are statically hoisted, ensuring imported bindings are available throughout the module.

35. What is the hoisting behavior of `export` statements in ES6 modules?

a) Export statements are hoisted along with their declarations to make module bindings accessible statically.
b) Exports are evaluated dynamically at the bottom of the file.
c) Exports are not hoisted.
d) Exports throw a SyntaxError if placed before declarations.
Correct Answer: a) Export statements are hoisted along with their declarations to make module bindings accessible statically.
Explanation:
Module exports are processed during static analysis and hoisting phases.

36. What happens when a function declaration is placed inside an `if` block in non-strict mode?

a) Legacy hoisting behaviors can cause the function declaration to hoist irregularly to the enclosing function or global scope, varying across engines.
b) It is strictly block-scoped without exception.
c) It throws an immediate SyntaxError.
d) It is ignored entirely.
Correct Answer: a) Legacy hoisting behaviors can cause the function declaration to hoist irregularly to the enclosing function or global scope, varying across engines.
Explanation:
In non-strict mode, block-level function declarations exhibit non-standard hoisting semantics, which strict mode resolves by restricting them to block scope.

37. What happens when a function declaration is placed inside an `if` block in strict mode (`'use strict'`)?

a) The function declaration is strictly block-scoped to the enclosing `if` block.
b) It leaks into the global window object.
c) It throws a SyntaxError during parsing.
d) It hoists to the top of the enclosing function.
Correct Answer: a) The function declaration is strictly block-scoped to the enclosing `if` block.
Explanation:
Strict mode standardizes block-level function declarations, confining them strictly to their enclosing block bounds.

38. Why did ES6 introduce `let` and `const` alongside `var` regarding hoisting confusion?

a) To provide predictable block scoping and eliminate the counterintuitive `undefined` initialization and scope leakage associated with `var` hoisting.
b) To make JavaScript compile into native machine code faster.
c) To replace functions entirely.
d) To enable multi-threading.
Correct Answer: a) To provide predictable block scoping and eliminate the counterintuitive `undefined` initialization and scope leakage associated with `var` hoisting.
Explanation:
Block scoping and the TDZ prevent bugs caused by premature variable access and scope leakage.

39. What is the output of `function test() { console.log(a); var a = 1; } test();`?

a) `undefined`
b) `1`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `undefined`
Explanation:
Inside function `test`, `var a` hoists to the top of the function scope and initializes with `undefined`.

40. What is the output of `function test() { console.log(a); let a = 1; } test();`?

a) `ReferenceError`
b) `undefined`
c) `1`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
The `let a` declaration creates a TDZ inside the function scope, causing a ReferenceError when accessed before initialization.

41. Can `const` variables be reassigned after hoisting and initialization?

a) No, `const` prevents variable reassignment after initial declaration.
b) Yes, once per block scope.
c) Yes, if declared inside a function.
d) Only in non-strict mode.
Correct Answer: a) No, `const` prevents variable reassignment after initial declaration.
Explanation:
Constants are read-only references once initialized.

42. Does declaring an object with `const` prevent its internal properties from being mutated after hoisting?

a) No, `const` prevents variable reassignment, but object properties remain fully mutable.
b) Yes, all nested properties become deeply frozen automatically.
c) Yes, object properties throw a TypeError if modified.
d) Only if declared in global scope.
Correct Answer: a) No, `const` prevents variable reassignment, but object properties remain fully mutable.
Explanation:
const locks the variable identifier reference, not the internal structural data of objects or arrays.

43. What is the hoisting behavior of variables declared inside a `try...catch` block's catch clause parameter?

a) The catch clause error identifier is block-scoped to the catch block and hoisted within it.
b) It leaks into the enclosing function scope like `var`.
c) It is not hoisted at all.
d) It hoists into the global scope.
Correct Answer: a) The catch clause error identifier is block-scoped to the catch block and hoisted within it.
Explanation:
ES6 catch parameters have their own block scope and hoisting rules.

44. What happens if you use the same identifier name for a function parameter and a `var` declaration inside the function body?

a) The `var` declaration is redundant because the parameter already initializes that identifier name in the function scope.
b) It throws an immediate SyntaxError.
c) The `var` declaration overwrites the parameter.
d) It creates two separate variables in memory.
Correct Answer: a) The `var` declaration is redundant because the parameter already initializes that identifier name in the function scope.
Explanation:
Parameters already occupy the function scope; redeclaring them with `var` does nothing harmful, though redeclaring with `let` throws an error.

45. What happens if you use the same identifier name for a function parameter and a `let` declaration inside the function body?

a) It throws a SyntaxError because `let` cannot redeclare an existing parameter identifier in the same scope.
b) It shadows the parameter successfully.
c) It overwrites the parameter value.
d) It ignores the `let` declaration.
Correct Answer: a) It throws a SyntaxError because `let` cannot redeclare an existing parameter identifier in the same scope.
Explanation:
Function parameters and `let` declarations share the function body scope, prohibiting duplicate identifier declarations.

46. How does the JavaScript engine handle variable lookups during execution if an identifier is hoisted?

a) It resolves the identifier from the current Lexical Environment's Environment Record, traversing up the scope chain if necessary.
b) It searches the HTML DOM tree.
c) It queries the network server.
d) It scans source code text files.
Correct Answer: a) It resolves the identifier from the current Lexical Environment's Environment Record, traversing up the scope chain if necessary.
Explanation:
Environment records store hoisted bindings for quick lookup during execution.

47. What is the output of `var a = 1; function b() { a = 10; return; function a() {} } b(); console.log(a);`?

a) `1`
b) `10`
c) `undefined`
d) `ReferenceError`
Correct Answer: a) `1`
Explanation:
Inside function `b`, function hoisting creates a local variable `a` (via `function a() {}`), shadowing global `a`. Reassigning `a = 10` modifies the local variable, leaving global `a` untouched at `1`.

48. What is variable shadowing in relation to scope and hoisting?

a) When an inner scope declares a variable with the same name as an outer scope variable, masking the outer variable within that inner region.
b) When garbage collection deletes an unreferenced variable.
c) When two variables share the same memory address.
d) When hoisting fails due to memory limits.
Correct Answer: a) When an inner scope declares a variable with the same name as an outer scope variable, masking the outer variable within that inner region.
Explanation:
Shadowing occurs when inner declarations take precedence over outer ones during scope chain resolution.

49. Can a hoisted function declaration be called before its variable assignment if assigned to a `var` variable later?

a) If a function declaration and a `var` share a name, the function declaration takes precedence during hoisting, making it callable immediately.
b) No, it always throws a TypeError.
c) Yes, but it returns `undefined`.
d) Only in strict mode.
Correct Answer: a) If a function declaration and a `var` share a name, the function declaration takes precedence during hoisting, making it callable immediately.
Explanation:
Function declarations override `var` declarations during hoisting, so the identifier points to the function initially.

50. What is the output of `console.log(x); var x = 5; function x() {} console.log(x);`?

a) `[Function: x]` then `5`
b) `undefined` then `5`
c) `5` then `5`
d) `ReferenceError`
Correct Answer: a) `[Function: x]` then `5`
Explanation:
During hoisting, `function x()` takes precedence over `var x`. The first log prints the function. Then `x = 5` reassigns the variable, so the second log prints `5`.

51. How do object methods defined with shorthand syntax (`obj = { myMethod() {} }`) behave during hoisting?

a) Object methods are part of object literals and are not hoisted; they are evaluated at runtime when the object is created.
b) They are hoisted to the top of the global scope.
c) They are hoisted as `undefined`.
d) They throw a SyntaxError.
Correct Answer: a) Object methods are part of object literals and are not hoisted; they are evaluated at runtime when the object is created.
Explanation:
Object literals and their methods are runtime expressions, not declarations subject to hoisting.

52. What is the hoisting behavior of `class` expressions (e.g., `const MyClass = class {}`)?

a) The variable identifier follows the hoisting rules of its declaration keyword (`var`, `let`, or `const`), while the class definition itself is not hoisted.
b) The class is fully hoisted like a class declaration.
c) Class expressions are never hoisted under any circumstance.
d) They cause a parsing syntax error.
Correct Answer: a) The variable identifier follows the hoisting rules of its declaration keyword (`var`, `let`, or `const`), while the class definition itself is not hoisted.
Explanation:
Class expressions behave like function expressions; only their container variable identifier follows hoisting rules.

53. What is the output of `var a = [typeof a, typeof b]; let b = 1; console.log(a);`?

a) `ReferenceError`
b) `["undefined", "number"]`
c) `["undefined", "undefined"]`
d) `["undefined", "function"]`
Correct Answer: a) `ReferenceError`
Explanation:
Evaluating `typeof b` while `b` is in the Temporal Dead Zone throws a ReferenceError, preventing array creation.

54. What is the primary purpose of hoisting in the design of JavaScript?

a) To allow mutually recursive functions to call each other regardless of their relative order in the source code file.
b) To eliminate the need for variable declarations entirely.
c) To make code execution run 10x faster.
d) To automatically manage garbage collection.
Correct Answer: a) To allow mutually recursive functions to call each other regardless of their relative order in the source code file.
Explanation:
Function hoisting was originally designed to facilitate calling functions before their declaration lines, making recursive function organization flexible.

55. What happens when you declare a variable with `var` inside a global script file in a browser regarding the window object?

a) It is hoisted and attached as a property of the global `window` object.
b) It remains entirely private to the script file.
c) It throws a ReferenceError in strict mode.
d) It defaults to block scope.
Correct Answer: a) It is hoisted and attached as a property of the global `window` object.
Explanation:
Global `var` declarations create properties on the global window object in browsers, whereas `let` and `const` do not.

56. What happens when you declare a variable with `let` or `const` in the global scope of a browser script?

a) It is hoisted into the global lexical environment but does not become a property of the global `window` object.
b) It attaches directly to `window` as a property.
c) It throws a SyntaxError.
d) It defaults to function scope.
Correct Answer: a) It is hoisted into the global lexical environment but does not become a property of the global `window` object.
Explanation:
Global `let` and `const` declarations avoid polluting global object properties.

57. What is the output of `console.log(a); var a = [1, 2, 3];`?

a) `undefined`
b) `[1, 2, 3]`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `undefined`
Explanation:
The `var a` declaration hoists and initializes with `undefined` before assignment occurs.

58. What is the output of `myFunc(); var myFunc = function() { console.log('Hi'); };`?

a) `TypeError: myFunc is not a function`
b) `Hi`
c) `ReferenceError`
d) `undefined`
Correct Answer: a) `TypeError: myFunc is not a function`
Explanation:
Var hoisting sets `var myFunc = undefined`. Invoking `myFunc()` attempts to call `undefined()`, throwing a TypeError.

59. How do default function parameters interact with scope and hoisting?

a) Default parameters evaluate in their own intermediate scope, meaning parameters defined earlier can be referenced by later default parameters.
b) Default parameters share scope with global variables.
c) Default parameters are not hoisted and throw an error.
d) Default parameters cannot access any variables.
Correct Answer: a) Default parameters evaluate in their own intermediate scope, meaning parameters defined earlier can be referenced by later default parameters.
Explanation:
Default parameter scope behaves like an intermediate block scope between function parameters and body.

60. What is the hoisting behavior of labeled statements in JavaScript?

a) Labels are identifiers used for loops and blocks and are not subject to variable hoisting.
b) Labels are hoisted to the top of the function scope.
c) Labels hoist like `var` declarations.
d) Labels throw a SyntaxError if hoisted.
Correct Answer: a) Labels are identifiers used for loops and blocks and are not subject to variable hoisting.
Explanation:
Labels are control flow markers, not variable or function declarations.

61. What is the output of `{ function foo() { return 1; } } console.log(foo());` in non-strict mode?

a) `1`
b) `ReferenceError`
c) `TypeError`
d) `undefined`
Correct Answer: a) `1`
Explanation:
In non-strict mode, block-level function declarations hoist to the function scope, making `foo` accessible outside the block.

62. What is the output of `"use strict"; { function foo() { return 1; } } console.log(foo());` in strict mode?

a) `ReferenceError`
b) `1`
c) `TypeError`
d) `undefined`
Correct Answer: a) `ReferenceError`
Explanation:
In strict mode, block-level function declarations are strictly block-scoped, throwing a ReferenceError when accessed outside the block.

63. Does hoisting apply to properties of objects or arrays?

a) No, hoisting applies exclusively to identifier declarations (`var`, `let`, `const`, `function`, `class`), not object or array properties.
b) Yes, all object properties are hoisted automatically.
c) Yes, array indexes are hoisted.
d) Only in strict mode.
Correct Answer: a) No, hoisting applies exclusively to identifier declarations (`var`, `let`, `const`, `function`, `class`), not object or array properties.
Explanation:
Property access and mutations occur strictly at runtime.

64. What happens if you declare a variable with `var` inside a loop without block scope awareness?

a) The variable is hoisted once to the outer function scope, sharing a single binding across all loop iterations.
b) A new variable is hoisted for every iteration.
c) It throws a ReferenceError.
d) It converts into a constant automatically.
Correct Answer: a) The variable is hoisted once to the outer function scope, sharing a single binding across all loop iterations.
Explanation:
Function-scoped `var` in loops shares one binding, which frequently causes closure bugs in asynchronous loops.

65. What happens when you use `let` in a `for` loop initialization header?

a) A fresh variable binding is created for each individual iteration loop cycle, preventing closure capture bugs.
b) It behaves identically to `var` with a single shared binding.
c) It throws a SyntaxError.
d) It hoists to the global scope.
Correct Answer: a) A fresh variable binding is created for each individual iteration loop cycle, preventing closure capture bugs.
Explanation:
Using `let` in loop heads creates per-iteration bindings, correctly preserving loop index values in callbacks.

66. What is the output of `var x = 1; function test() { console.log(x); var x = 2; } test();`?

a) `undefined`
b) `1`
c) `2`
d) `ReferenceError`
Correct Answer: a) `undefined`
Explanation:
Local `var x` hoists to the top of function `test`, shadowing global `x = 1` and initializing as `undefined` before logging.

67. What is the output of `let x = 1; function test() { console.log(x); let x = 2; } test();`?

a) `ReferenceError`
b) `1`
c) `2`
d) `undefined`
Correct Answer: a) `ReferenceError`
Explanation:
Local `let x` creates a TDZ inside function `test`, throwing a ReferenceError when accessed before initialization instead of reading outer `x = 1`.

68. Can function expressions be hoisted if assigned to `var` inside another function?

a) Only the `var` identifier is hoisted as `undefined`; the function expression assignment occurs at runtime when execution reaches that line.
b) The entire function is fully hoisted.
c) It throws a SyntaxError.
d) It hoists into the global scope.
Correct Answer: a) Only the `var` identifier is hoisted as `undefined`; the function expression assignment occurs at runtime when execution reaches that line.
Explanation:
Function expression assignments never hoist their values, only their container variable identifiers.

69. What is the output of `console.log(foo); var foo = 2;`?

a) `undefined`
b) `2`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `undefined`
Explanation:
Var hoisting initializes `foo` as `undefined` during compilation.

70. What is the output of `console.log(foo); let foo = 2;`?

a) `ReferenceError`
b) `undefined`
c) `2`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
Let hoisting leaves `foo` uninitialized in the TDZ, throwing a ReferenceError.

71. What is the output of `console.log(foo); const foo = 2;`?

a) `ReferenceError`
b) `undefined`
c) `2`
d) `TypeError`
Correct Answer: a) `ReferenceError`
Explanation:
Const hoisting leaves `foo` uninitialized in the TDZ, throwing a ReferenceError.

72. What is the output of `bar(); var bar = function() { console.log(1); }`?

a) `TypeError: bar is not a function`
b) `1`
c) `ReferenceError`
d) `undefined`
Correct Answer: a) `TypeError: bar is not a function`
Explanation:
Var hoisting initializes `bar` as `undefined`. Calling `bar()` evaluates `undefined()`, throwing a TypeError.

73. How do nested functions interact with hoisting inside outer functions?

a) Nested function declarations are fully hoisted to the top of their enclosing outer function scope.
b) Nested functions are not hoisted until the outer function finishes executing.
c) Nested functions hoist into the global scope.
d) Nested functions throw a SyntaxError.
Correct Answer: a) Nested function declarations are fully hoisted to the top of their enclosing outer function scope.
Explanation:
Function declarations hoist to the top of their immediate function or global container scope.

74. What happens when you declare a variable with `var` inside a `try` block and access it in the `catch` block?

a) It is accessible because `var` is function-scoped and ignores block boundaries.
b) It throws a ReferenceError because `try` creates a block scope.
c) It returns `undefined`.
d) It is deleted by garbage collection.
Correct Answer: a) It is accessible because `var` is function-scoped and ignores block boundaries.
Explanation:
Var declarations ignore try/catch block boundaries.

75. What happens when you declare a variable with `let` inside a `try` block and access it in the `catch` block?

a) It throws a ReferenceError because `let` is strictly block-scoped to the `try` block.
b) It is accessible across both blocks.
c) It returns `undefined`.
d) It defaults to global scope.
Correct Answer: a) It throws a ReferenceError because `let` is strictly block-scoped to the `try` block.
Explanation:
Let variables declared in a try block cannot be accessed outside that block.

76. Can a `const` variable be declared without an initial value during hoisting?

a) No, declaring a `const` variable without initialization throws a SyntaxError during parsing.
b) Yes, it defaults to `undefined`.
c) Yes, if assigned later in the block.
d) Only inside constructor methods.
Correct Answer: a) No, declaring a `const` variable without initialization throws a SyntaxError during parsing.
Explanation:
Constants require immediate initialization at declaration time.

77. What is the output of `var x = 5; (function() { console.log(x); var x = 10; })();`?

a) `undefined`
b) `5`
c) `10`
d) `ReferenceError`
Correct Answer: a) `undefined`
Explanation:
The IIFE declares local `var x`, which hoists to the top of the IIFE scope and initializes as `undefined`, shadowing global `x = 5`.

78. What is the output of `let x = 5; (function() { console.log(x); let x = 10; })();`?

a) `ReferenceError`
b) `5`
c) `10`
d) `undefined`
Correct Answer: a) `ReferenceError`
Explanation:
The IIFE declares local `let x`, creating a TDZ for that function scope and throwing a ReferenceError when accessed early.

79. How does hoisting handle duplicate function declarations in the exact same scope?

a) The later function declaration overwrites the earlier function declaration during hoisting.
b) It throws a SyntaxError for duplicate identifiers.
c) Both functions execute simultaneously.
d) The first function declaration takes precedence.
Correct Answer: a) The later function declaration overwrites the earlier function declaration during hoisting.
Explanation:
When duplicate function declarations occur in the same scope, the subsequent declaration overwrites the previous one during hoisting.

80. What is the output of `foo(); function foo() { console.log(1); } function foo() { console.log(2); }`?

a) `2`
b) `1`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `2`
Explanation:
The second `foo` definition overwrites the first during hoisting, so invoking `foo()` prints `2`.

81. Does hoisting occur inside Web Workers or background threads in JavaScript?

a) Yes, every independent JavaScript execution context (including Web Workers) performs hoisting during its compilation phase.
b) No, Web Workers disable hoisting.
c) Only main threads support hoisting.
d) Only in browser environments, not Node.js workers.
Correct Answer: a) Yes, every independent JavaScript execution context (including Web Workers) performs hoisting during its compilation phase.
Explanation:
All JS execution contexts follow the same compilation and hoisting principles.

82. What is the output of `console.log(a); var a = function a() { return 1; };`?

a) `undefined`
b) `[Function: a]`
c) `ReferenceError`
d) `TypeError`
Correct Answer: a) `undefined`
Explanation:
This is a named function expression, not a function declaration. Only the `var a` identifier hoists, initializing as `undefined`.

83. What is the output of `console.log(a); const a = 10;`?

a) `ReferenceError`
b) `undefined`
c) `10`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
Const declarations reside in the TDZ, throwing a ReferenceError when accessed before initialization.

84. What is the output of `console.log(a); let a = 10;`?

a) `ReferenceError`
b) `undefined`
c) `10`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
Let declarations reside in the TDZ, throwing a ReferenceError when accessed before initialization.

85. What is the output of `console.log(a); { var a = 10; } console.log(a);`?

a) `undefined` then `10`
b) `ReferenceError` then `10`
c) `10` then `10`
d) `undefined` then `undefined`
Correct Answer: a) `undefined` then `10`
Explanation:
Var ignores block scope, hoisting to the outer/global scope. First log prints `undefined`, assignment runs, second log prints `10`.

86. What is the output of `console.log(a); { let a = 10; }`?

a) `ReferenceError`
b) `undefined`
c) `10`
d) `null`
Correct Answer: a) `ReferenceError`
Explanation:
The first `console.log(a)` attempts to read `a` in outer scope where it does not exist, throwing a ReferenceError.

87. How do generator function expressions behave during hoisting?

a) Only their variable container identifier hoists according to its declaration keyword (`var`, `let`, or `const`), while the generator expression assignment runs at runtime.
b) They are fully hoisted like generator declarations.
c) They are never hoisted.
d) They throw a SyntaxError.
Correct Answer: a) Only their variable container identifier hoists according to its declaration keyword (`var`, `let`, or `const`), while the generator expression assignment runs at runtime.
Explanation:
Generator expressions follow standard variable expression hoisting rules.

88. What is the output of `var x = 10; function test(x = x) { return x; } test();`?

a) `ReferenceError` (because default parameter `x = x` attempts to reference `x` while it is in its own Temporal Dead Zone)
b) `10`
c) `undefined`
d) `null`
Correct Answer: a) `ReferenceError` (because default parameter `x = x` attempts to reference `x` while it is in its own Temporal Dead Zone)
Explanation:
Default parameters evaluate in their own scope where parameters act like `let` declarations, meaning referencing `x` before initialization triggers a TDZ ReferenceError.

89. Why is understanding hoisting essential for JavaScript developers?

a) It helps prevent subtle bugs, avoids unexpected `undefined` variable values, clarifies scope boundaries, and aids in debugging technical interview questions.
b) It is only required for writing V8 engine source code.
c) It replaces asynchronous event loop handling.
d) It is exclusively needed for CSS styling.
Correct Answer: a) It helps prevent subtle bugs, avoids unexpected `undefined` variable values, clarifies scope boundaries, and aids in debugging technical interview questions.
Explanation:
Mastering hoisting provides deep insight into JavaScript execution contexts, compilation phases, and variable scope resolution.
← Previous: JavaScript Event Handling MCQs
Next →: JavaScript Interview Questions MCQs
NewJavaScript This Keyword & Scope

JavaScript This Keyword & Scope MCQs

Variable scoping and declaration keywords (var, let, and const) determine where identifiers are visible and accessible within a JavaScript execution…

By MCQs Generator
NewPython Functions MCQs

Python Functions MCQs

Functions in Python are reusable blocks of code designed to perform a specific task, promoting code modularity, readability, and DRY…

By MCQs Generator
NewJavaScript Prototypes & Inheritance MCQs for Developer Interviews

JavaScript Prototypes & Inheritance MCQs for Developer Interviews

JavaScript implements inheritance exclusively through a prototype-based model rather than traditional class-based mechanics found in languages like Java or C++.…

By MCQs Generator