a) Raising all variables to the top of their function scope
b) Moving function declarations to the top of the script
c) Lifting up all variable and function declarations to the top of their containing scope
d) Sorting variables alphabetically
Answer:
c) Lifting up all variable and function declarations to the top of their containing scope
Explanation:
Hoisting in JavaScript refers to the behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase.
2. Which types of declarations are hoisted in JavaScript?
a) var declarations and function expressions
b) let and const declarations
c) var declarations and function declarations
d) Only function declarations
Answer:
c) var declarations and function declarations
Explanation:
In JavaScript, var declarations and function declarations are hoisted, meaning they are moved to the top of their containing scope.
3. How are variable declarations hoisted in JavaScript?
a) The declaration and initialization are hoisted
b) Only the declaration is hoisted, not the initialization
c) The initialization is hoisted, but not the declaration
d) Neither the declaration nor the initialization is hoisted
Answer:
b) Only the declaration is hoisted, not the initialization
Explanation:
When variables are hoisted in JavaScript, only the declaration is hoisted to the top of the scope, not the initialization.
4. What is the initial value of a variable that is hoisted in JavaScript?
a) 0
b) null
c) undefined
d) An empty string
Answer:
c) undefined
Explanation:
A hoisted variable in JavaScript is initialized with a value of undefined.
5. Can a function expression be hoisted in JavaScript?
a) Yes, always
b) No, function expressions are not hoisted
c) Yes, but only if it's assigned to a var variable
d) Yes, but only if it's assigned to a let or const variable
Answer:
b) No, function expressions are not hoisted
Explanation:
Function expressions, unlike function declarations, are not hoisted in JavaScript.
6. How does hoisting affect let and const declarations?
a) They are hoisted to the top of the block
b) They are not hoisted
c) They are hoisted to the top of the script
d) They are hoisted, but remain uninitialized
Answer:
d) They are hoisted, but remain uninitialized
Explanation:
let and const declarations are hoisted to the top of their block scope but are not initialized, resulting in a temporal dead zone until the line where they are declared.
7. What is the result of accessing a hoisted function before its declaration?
a) It executes normally
b) It returns undefined
c) It causes a ReferenceError
d) It returns null
Answer:
a) It executes normally
Explanation:
Due to hoisting, a function can be called in JavaScript even before its declaration in the code.
8. How do let and const behave differently from var in terms of hoisting?
a) They are hoisted to the global scope
b) They are not hoisted
c) They create properties on the global object
d) They are hoisted but inaccessible until their declaration
Answer:
d) They are hoisted but inaccessible until their declaration
Explanation:
let and const are hoisted to the top of their block scope but are not accessible until the line where they are declared, unlike var, which is accessible and initialized as undefined.
9. What happens when a var variable is hoisted and accessed before its initialization?
a) It returns null
b) It returns its initialized value
c) It causes a TypeError
d) It returns undefined
Answer:
d) It returns undefined
Explanation:
Accessing a var variable before its initialization due to hoisting will return undefined, as the variable is hoisted and initialized with undefined.
10. Are class declarations hoisted in JavaScript?
a) Yes, like function declarations
b) No, they are not hoisted
c) Yes, but only if they are anonymous
d) Yes, but they are not initialized
Answer:
b) No, they are not hoisted
Explanation:
Class declarations in JavaScript are not hoisted, meaning they cannot be used before they are declared in the code.
11. What is the temporal dead zone in JavaScript?
a) The time when a script is not running
b) The time between variable hoisting and its declaration
c) The time after a function is executed
d) The time before a script is loaded
Answer:
b) The time between variable hoisting and its declaration
Explanation:
The temporal dead zone refers to the time from when a block begins until let and const variables are declared within that block. During this time, the variables are in scope but not yet initialized.
12. How are named function expressions hoisted?
a) The function name is hoisted
b) The function name and body are hoisted
c) Only the variable name is hoisted
d) Neither the function name nor the body is hoisted
Answer:
c) Only the variable name is hoisted
Explanation:
For named function expressions, only the variable name is hoisted, not the function name or the body.
13. Can you redeclare a variable using var in the same scope?
a) Yes, without any issues
b) No, it causes a syntax error
c) Yes, but it overwrites the previous value
d) No, it returns undefined
Answer:
a) Yes, without any issues
Explanation:
In JavaScript, redeclaring a variable using var in the same scope is allowed and does not cause an error.
14. How does hoisting work inside a function scope?
a) Variables are hoisted to the top of the script
b) Variables and functions are hoisted to the top of their function scope
c) Hoisting does not occur in function scopes
d) Only functions are hoisted, not variables
Answer:
b) Variables and functions are hoisted to the top of their function scope
Explanation:
Inside a function scope, both variables (declared with var) and function declarations are hoisted to the top of their function scope.
15. What is the effect of hoisting on variables declared within a for loop?
a) They are hoisted to the global scope
b) They are hoisted to the top of the function scope
c) They are hoisted to the top of the loop block
d) They are not hoisted
Answer:
b) They are hoisted to the top of the function scope
Explanation:
If a variable is declared with var within a for loop, it is hoisted to the top of the function scope in which the loop resides.