Hoisting and Variable Output
Given the following JavaScript code:nnconsole.log(variable_1);nvar variable_1 = 10;nif (true) { var variable_2 = 20; }nconsole.log(variable_2);nnWhat will be printed to the console?
- A. undefined, 20
- B. 10, undefined
- C. ReferenceError, 20
- D. undefined, undefined
- E. 10, 20
Function Scope and Let Keyword
Consider this loop:nnfor (let i = 0; i u003C 5; i++) {n setTimeout(() =u003E console.log(i), 0);n}nnWhat values will be printed to the console and in what order?
- A. 0, 1, 2, 3, 4
- B. 5, 5, 5, 5, 5
- C. 0, 1, 2, 3, 4 in reverse
- D. undefined, undefined, undefined, undefined, undefined
- E. 1, 2, 3, 4, 5
Closures and Persistent Counters
You are asked to write a function that returns a function which increments and returns a private counter each time it's called. Which concept makes this possible in JavaScript?
- A. Closure
- B. Event Loop
- C. Hoisting
- D. Currying
- E. Block Scope
Closures Gone Wrong
Given the following code:nnfor(var n=1; nu003C=5; n++){n setTimeout(function(){ console.log('n = ' + n); }, 1000);n}nnWhat will be printed after one second?
- A. n = 1, n = 2, n = 3, n = 4, n = 5
- B. n = 5, n = 5, n = 5, n = 5, n = 5
- C. n = 0, n = 1, n = 2, n = 3, n = 4
- D. Undefined, five times
- E. n = 4, n = 3, n = 2, n = 1, n = 0
Hoisting Nuances with Let and Var
Given:nnvar a = 35;nfunction test() {n console.log(a);n let a = 10;n console.log(a);n}ntest();nnWhat will be the output?
- A. 35, 10
- B. ReferenceError, 10
- C. undefined, 10
- D. 10, 35
- E. 10, undefined
Understanding 'this' with Arrow Functions
Given the following code:nnconst obj = {n value: 42,n regular: function() { return this.value; },n arrow: () =u003E this.valuen};nnWhat will obj.regular() and obj.arrow() respectively return?
- A. 42, undefined
- B. undefined, 42
- C. 42, 42
- D. undefined, undefined
- E. 0, 42
Difference in Object Creation
Which of the following statements correctly describes the difference between creating an object with {} and using Object.create(null)?
- A. Object.create(null) creates an object with no prototype; {} creates an object with Object.prototype.
- B. Both methods create objects with no prototype.
- C. {} creates an object with Array.prototype; Object.create(null) does not.
- D. Object.create(null) creates an object with primitive properties only.
- E. {} is slower than Object.create(null).
Alternative to Closures
If JavaScript did not support closures, which of the following would be most affected?
- A. The ability to create private variables in functions
- B. Hosting external scripts
- C. Prototypical inheritance
- D. Variable hoisting
- E. Global scope manipulation
Output Prediction with Variable Hoisting
Consider this example:nnconsole.log(a);nvar a = 10;nfunction foo() {n console.log(a);n var a = 15;n bar();n function bar() { console.log('foo:' + a + ' bar:' + b); }n var b = 15;n}nfoo();nnWhat will be printed?
- A. undefined, undefined, foo:15 bar:15
- B. undefined, undefined, foo:undefined bar:15
- C. undefined, undefined, foo:undefined bar:undefined
- D. undefined, undefined, foo:10 bar:15
- E. undefined, undefined, foo:1 bar:15
Pros and Cons of Closures
Which of the following correctly states a disadvantage of using closures excessively in JavaScript applications?
- A. They can lead to higher memory usage due to retained references.
- B. They prevent variable hoisting.
- C. Closures cannot be used with arrow functions.
- D. They disable asynchronous execution.
- E. They only allow primitive data types to be captured.