Understanding Hoisting in Variable Declarations
Given the following code:nconsole.log(a);nvar a = 10;nconsole.log(a);nWhat will be the output?
- A) undefined, 10
- B) 10, 10
- C) ReferenceError, 10
- D) null, 10
- E) 0, 10
Function Hoisting with Declarations and Expressions
Suppose the following snippet:nconsole.log(foo());nfunction foo() { return 42; }nWhat is the output?
- A) 42
- B) undefined
- C) ReferenceError: foo is not defined
- D) TypeError: foo is not a function
- E) null
The subtleties of let in for-loops and Closures
What is printed to the console by this code?nfor(let i = 0; i u003C 5; i++) {n setTimeout(() =u003E console.log(i), 0);n}
- A) 0 1 2 3 4
- B) 5 5 5 5 5
- C) 0 0 0 0 0
- D) 1 2 3 4 5
- E) undefined undefined undefined undefined undefined
Classic Closure Trap with var
Given the following code:nfor(var i=1; iu003C=5; i++) {n setTimeout(() =u003E console.log('n = ' + i), 0);n}nWhat is printed to the console?
- A) n = 5 five times
- B) n = 6 five times
- C) n = 1, n = 2, n = 3, n = 4, n = 5
- D) n = 0 five times
- E) n = undefined five times
Hoisting Order Between Functions and Variables
Given:nconsole.log(bar);nfunction bar() {}nvar bar = 10;nconsole.log(bar);nWhat is the output?
- A) function bar() {}, 10
- B) undefined, 10
- C) 10, 10
- D) ReferenceError: bar is not defined
- E) null, 10
Understanding the 'this' keyword in Arrow Functions
Suppose you have the following object:nconst obj = {n value: 100,n getValue: () =u003E this.valuen};nWhat will obj.getValue() return?
- A) undefined
- B) 100
- C) [object Object]
- D) Error: this is not defined
- E) NaN
Prototype vs. Object Creation
What is the main difference between using 'new Object()' and 'Object.create(null)' to create an object in JavaScript?
- A) Object.create(null) creates an object with no prototype, while new Object() creates an object with Object.prototype.
- B) new Object() creates an object with no prototype, while Object.create(null) creates one with Object.prototype.
- C) Both create objects with Object.prototype by default.
- D) Object.create(null) is invalid syntax.
- E) There is no difference; both expressions are equivalent.
Variable Shadowing and Scope
Given the code:nlet x = 10;nfunction test() {n let x = 20;n console.log(x);n}ntest();nconsole.log(x);nWhat will be logged?
- A) 20, 10
- B) 10, 20
- C) 20, 20
- D) undefined, 10
- E) 10, undefined
Temporal Dead Zone with let and const
What happens if you try to access a variable declared with 'let' before its declaration within the same scope?
- A) ReferenceError is thrown due to temporal dead zone.
- B) undefined is returned due to hoisting.
- C) null is assigned automatically.
- D) NaN is logged.
- E) It works as with variables declared with var.
Closures for Private Variables
Which code correctly implements a private counter in JavaScript using closure so that only the increment function can access the value?
- A) function counter() { let c = 0; return { increment: () =u003E ++c }; }
- B) function counter() { this.c = 0; this.increment = function() { return ++this.c }; }
- C) function counter() { var c = 0; return { inc: () =u003E c++ }; }
- D) function counter() { count = 0; return { increment: () =u003E ++count }; }
- E) function counter() { const c = 0; return { inc: () =u003E c++ }; }