JavaScript Mastermind: 10 Tricky Interview Questions You Must Conquer Quiz

  1. Understanding Hoisting in Variable Declarations

    Given the following code:nconsole.log(a);nvar a = 10;nconsole.log(a);nWhat will be the output?

    1. A) undefined, 10
    2. B) 10, 10
    3. C) ReferenceError, 10
    4. D) null, 10
    5. E) 0, 10
  2. Function Hoisting with Declarations and Expressions

    Suppose the following snippet:nconsole.log(foo());nfunction foo() { return 42; }nWhat is the output?

    1. A) 42
    2. B) undefined
    3. C) ReferenceError: foo is not defined
    4. D) TypeError: foo is not a function
    5. E) null
  3. 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}

    1. A) 0 1 2 3 4
    2. B) 5 5 5 5 5
    3. C) 0 0 0 0 0
    4. D) 1 2 3 4 5
    5. E) undefined undefined undefined undefined undefined
  4. 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?

    1. A) n = 5 five times
    2. B) n = 6 five times
    3. C) n = 1, n = 2, n = 3, n = 4, n = 5
    4. D) n = 0 five times
    5. E) n = undefined five times
  5. Hoisting Order Between Functions and Variables

    Given:nconsole.log(bar);nfunction bar() {}nvar bar = 10;nconsole.log(bar);nWhat is the output?

    1. A) function bar() {}, 10
    2. B) undefined, 10
    3. C) 10, 10
    4. D) ReferenceError: bar is not defined
    5. E) null, 10
  6. 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?

    1. A) undefined
    2. B) 100
    3. C) [object Object]
    4. D) Error: this is not defined
    5. E) NaN
  7. Prototype vs. Object Creation

    What is the main difference between using 'new Object()' and 'Object.create(null)' to create an object in JavaScript?

    1. A) Object.create(null) creates an object with no prototype, while new Object() creates an object with Object.prototype.
    2. B) new Object() creates an object with no prototype, while Object.create(null) creates one with Object.prototype.
    3. C) Both create objects with Object.prototype by default.
    4. D) Object.create(null) is invalid syntax.
    5. E) There is no difference; both expressions are equivalent.
  8. 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?

    1. A) 20, 10
    2. B) 10, 20
    3. C) 20, 20
    4. D) undefined, 10
    5. E) 10, undefined
  9. 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?

    1. A) ReferenceError is thrown due to temporal dead zone.
    2. B) undefined is returned due to hoisting.
    3. C) null is assigned automatically.
    4. D) NaN is logged.
    5. E) It works as with variables declared with var.
  10. 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?

    1. A) function counter() { let c = 0; return { increment: () =u003E ++c }; }
    2. B) function counter() { this.c = 0; this.increment = function() { return ++this.c }; }
    3. C) function counter() { var c = 0; return { inc: () =u003E c++ }; }
    4. D) function counter() { count = 0; return { increment: () =u003E ++count }; }
    5. E) function counter() { const c = 0; return { inc: () =u003E c++ }; }