javascript-interview-prep/lessons/00/quiz-difficulty-medium.md

8.9 KiB
Raw Permalink Blame History

Lesson 00: Quiz - Medium Difficulty

Time: 10-15 minutes Questions: 12 Passing Score: 9/12 (75%)


Instructions

Choose the best answer for each question. After completing, check your answers at the bottom.


Question 1

What is the output?

const x = null;
const y = undefined;
console.log(typeof x === typeof y);
  • A) true
  • B) false
  • C) TypeError
  • D) undefined

Your answer: ___


Question 2

What will be logged?

const a = [1, 2, 3];
const b = a;
const c = [1, 2, 3];
console.log(a === b, a === c);
  • A) true, true
  • B) true, false
  • C) false, true
  • D) false, false

Your answer: ___


Question 3

What is the result?

const num1 = 0.1;
const num2 = 0.2;
const num3 = 0.3;
console.log(num1 + num2 === num3);
  • A) true
  • B) false
  • C) undefined
  • D) NaN

Your answer: ___


Question 4

What will be logged?

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
  • A) 0, 1, 2
  • B) 3, 3, 3
  • C) 0, 0, 0
  • D) undefined, undefined, undefined

Your answer: ___


Question 5

What is the output?

const arr = [1, 2];
arr[5] = 6;
console.log(arr.length, arr[3]);
  • A) 3, undefined
  • B) 6, undefined
  • C) 6, null
  • D) 3, null

Your answer: ___


Question 6

What gets logged?

const value = +"123abc";
console.log(value, typeof value);
  • A) 123, "number"
  • B) "123abc", "string"
  • C) NaN, "number"
  • D) undefined, "undefined"

Your answer: ___


Question 7

What is the result?

console.log([] + []);
console.log({} + {});
  • A) "", "[object Object][object Object]"
  • B) [], {}
  • C) "", {}
  • D) undefined, undefined

Your answer: ___


Question 8

What will be logged?

function test() {
  console.log(x);
  let x = 10;
}
test();
  • A) undefined
  • B) 10
  • C) ReferenceError
  • D) null

Your answer: ___


Question 9

What is the output?

const obj = { x: 1, y: 2, x: 3 };
console.log(obj.x);
  • A) 1
  • B) 2
  • C) 3
  • D) SyntaxError

Your answer: ___


Question 10

What gets logged?

let x = y = 10;
function foo() {
  let x = y = 20;
}
foo();
console.log(y);
  • A) 10
  • B) 20
  • C) undefined
  • D) ReferenceError

Your answer: ___


Question 11

What is the result?

const arr = new Array(3);
console.log(arr.length, arr[0], 0 in arr);
  • A) 3, undefined, true
  • B) 3, undefined, false
  • C) 0, undefined, false
  • D) 3, null, false

Your answer: ___


Question 12

What will be logged?

function getData() {
  return
  {
    value: 42
  };
}
console.log(getData());
  • A) { value: 42 }
  • B) undefined
  • C) 42
  • D) SyntaxError

Your answer: ___


Answer Key

Question 1: B - false

Explanation: typeof null returns "object" (a JavaScript bug), while typeof undefined returns "undefined". Therefore "object" === "undefined" is false.

console.log(typeof null);      // "object"
console.log(typeof undefined); // "undefined"

Question 2: B - true, false

Explanation:

  • a === b is true because b references the same array object as a
  • a === c is false because c is a different array object in memory, even though it has the same values
  • Arrays (and objects) are compared by reference, not by value
const a = [1, 2, 3];
const b = a;        // b points to same array
const c = [1, 2, 3]; // c is a new array

Question 3: B - false

Explanation: Due to floating-point precision issues, 0.1 + 0.2 equals 0.30000000000000004, not exactly 0.3. Never use === to compare decimal numbers. Use epsilon comparison or Number.EPSILON instead.

console.log(0.1 + 0.2);                           // 0.30000000000000004
console.log(Math.abs(0.1 + 0.2 - 0.3) < 0.0001); // true (correct way)

Question 4: B - 3, 3, 3

Explanation: With var, the variable i is function-scoped, not block-scoped. All three timeout callbacks share the same i variable. By the time they execute (after the loop completes), i has the value 3.

Fix with let:

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0); // 0, 1, 2
}
// `let` creates a new binding for each iteration

Question 5: B - 6, undefined

Explanation:

  • Array length is always the highest index + 1
  • Setting arr[5] = 6 makes the length 6
  • Indices 2, 3, 4 are "holes" (empty slots)
  • Accessing a hole returns undefined
const arr = [1, 2];
arr[5] = 6;
console.log(arr); // [1, 2, empty × 3, 6]

Question 6: C - NaN, "number"

Explanation: The unary plus operator (+) attempts to convert the value to a number. "123abc" cannot be converted to a valid number, so it results in NaN (Not a Number). Importantly, NaN has type "number".

console.log(+"123");    // 123
console.log(+"hello");  // NaN
console.log(typeof NaN); // "number"

Question 7: A - "", "[object Object][object Object]"

Explanation:

  • Empty arrays convert to empty strings: [] + []"" + """"
  • Objects convert to "[object Object]": {} + {}"[object Object][object Object]"
  • The + operator performs string concatenation when either operand is a string

Note: In some contexts (like at the start of a statement), {} might be interpreted as a code block, giving different results.


Question 8: C - ReferenceError

Explanation: Variables declared with let and const are in the "temporal dead zone" from the start of the block until the declaration is executed. Accessing them before the declaration line throws a ReferenceError, not undefined like with var.

// With var (hoisted):
function test() {
  console.log(x); // undefined
  var x = 10;
}

// With let (TDZ):
function test() {
  console.log(x); // ReferenceError
  let x = 10;
}

Question 9: C - 3

Explanation: When an object has duplicate keys, the last value wins. There's no error—the property is simply overwritten. obj.x will be 3.

const obj = { a: 1, a: 2, a: 3 };
console.log(obj.a); // 3

Question 10: B - 20

Explanation: The expression let x = y = 20 is evaluated as let x = (y = 20). The y = 20 part is an assignment without a declaration, creating (or modifying) a global variable y. Inside the function, x is local, but y becomes global.

let x = y = 10;     // x is local to outer scope, y is GLOBAL
function foo() {
  let x = y = 20;   // x is local to foo, y is GLOBAL (modified)
}
foo();
console.log(y);     // 20 (global y was changed)
console.log(x);     // 10 (outer x unaffected)

Fix: Always declare variables explicitly:

let x = 10, y = 10;

Question 11: B - 3, undefined, false

Explanation:

  • new Array(3) creates an array with length 3 but no elements (all holes)
  • arr[0] returns undefined (accessing a hole)
  • 0 in arr returns false because index 0 doesn't actually exist (it's a hole, not a slot containing undefined)
const arr1 = new Array(3);
const arr2 = [undefined, undefined, undefined];

console.log(0 in arr1); // false (hole)
console.log(0 in arr2); // true (actual value)

Question 12: B - undefined

Explanation: Automatic Semicolon Insertion (ASI) adds a semicolon after return, making it return;. The object literal on the next line becomes unreachable code. The function returns undefined.

What JavaScript sees:

function getData() {
  return; // semicolon inserted!
  {
    value: 42
  };
}

Fix: Keep the opening brace on the same line:

function getData() {
  return {
    value: 42
  };
}

Scoring Guide

  • 11-12 correct (90%+): Excellent! You have a strong grasp of JavaScript fundamentals.
  • 9-10 correct (75-83%): Good understanding. Review missed concepts before proceeding.
  • 7-8 correct (58-67%): Fair. Review this lesson's knowledge.md carefully.
  • Below 7 (< 58%): Need more practice. Re-read the knowledge section and practice with code examples.

Common Mistakes to Review

If you missed these questions, focus on these topics:

  • Q1, Q6: Type system and typeof operator
  • Q2: Reference vs value comparison
  • Q3: Floating-point precision
  • Q4, Q8: var vs let differences and hoisting
  • Q5, Q11: Array holes and length behavior
  • Q7: Type coercion with operators
  • Q9, Q12: JavaScript quirks (duplicate keys, ASI)
  • Q10: Implicit global variables

Ready for the Next Lesson?

If you scored 9 or higher, you're ready to proceed to Lesson 01: Type System & Coercion.

If you scored below 9, consider:

  1. Re-reading the sections you struggled with in knowledge.md
  2. Running the code examples yourself
  3. Retaking this quiz

Remember: Understanding these fundamentals is crucial for the more advanced topics ahead!