javascript-interview-prep/exams/00-evaluation-answers.md

4.7 KiB

Answer Key - JavaScript Diagnostic Assessment

Correct Answers with Explanations

Section 1: Core Concepts & Fundamentals

1. B - "object"

  • This is a famous JavaScript quirk. typeof null returns "object" due to a legacy bug in JavaScript that has never been fixed for backwards compatibility.

2. C - 11

  • When you assign to index 10, JavaScript creates a sparse array with length 11 (indices 0-10, even though 3-9 are empty slots).

3. B - undefined

  • When you extract a method from an object and call it as a standalone function, this becomes undefined in strict mode or the global object in non-strict mode. Since bar doesn't exist there, it's undefined.

4. B - false

  • Floating-point arithmetic precision issue. 0.1 + 0.2 equals 0.30000000000000004, not exactly 0.3.

Section 2: Closures & Scope

5. B - 3, 3, 3

  • var is function-scoped, not block-scoped. By the time the timeouts execute, the loop has finished and i is 3. All three callbacks reference the same i.

6. B - 3, 4

  • The inner function closes over the x variable from outer's scope. Each call increments and logs it: first 2→3, then 3→4.

Section 3: Promises & Async

7. C - 1, 4, 3, 2

  • Synchronous code runs first (1, 4), then microtasks/promises (3), then macrotasks/setTimeout (2).

8. B - Promise {: "hello"}

  • Async functions always return a Promise. The promise is immediately fulfilled with "hello".

9. C - 3

  • The promise chain: 1 → 2 → error is thrown → caught and returns 3 → logs 3.

Section 4: Modern ES6+ Features

10. C - 3

  • Duplicate keys in object literals are allowed. The last value wins.

11. D - SyntaxError

  • Rest element must be the last element in destructuring. You can't have ...b followed by ,c.

12. A - 1

  • Spread operator creates a shallow copy. Modifying the copy doesn't affect the original.

Section 5: Objects & Prototypes

13. D - TypeError

  • Object.create(null) creates an object with no prototype. It doesn't inherit toString() from Object.prototype, so calling it throws a TypeError.

14. B - false

  • greet is on the prototype chain, not an own property. hasOwnProperty returns false.

15. C - ReferenceError

  • You must call super() before accessing this in a derived class constructor. This code throws "ReferenceError: Must call super constructor before accessing 'this'".

Section 6: Array Methods & Iteration

16. A - [6, 8]

  • map produces [2, 4, 6, 8], then filter keeps only values > 5, which are [6, 8].

17. A - 6

  • Reduce without initial value uses first element as accumulator: 1 + 2 = 3, 3 + 3 = 6.

18. A - 1, 3

  • return in forEach only exits that iteration, not the whole loop. So 1 is logged, 2 is skipped, 3 is logged.

Section 7: Event Loop & Execution

19. B - 1, 3, 2

  • "1" logs synchronously, then "3" logs (synchronous after function call), then "2" logs (after await in microtask queue).

20. B - 1, 4, 2, 3

  • Both queueMicrotask and Promise.then use the microtask queue and execute in order. Synchronous code (1, 4) runs first, then microtasks (2, 3) in order.

Section 8: Tricky Scenarios & Edge Cases

21. B - false

  • Arrays are objects and compared by reference, not value. These are two different array instances.

22. B - ""

  • When arrays are coerced to strings for concatenation, [] → "", so "" + "" = "".

23. B - undefined

  • Arrow functions don't have their own this binding. They inherit this from the enclosing scope (likely global/window), where value doesn't exist.

24. B - 42

  • Unary + operator converts string to number: +"42" becomes 42.

25. B - 20

  • y becomes a global variable because y = 10 (and later y = 20) is an assignment without declaration. Inside the function, y = 20 updates the global y.

Scoring Guide

  • 23-25 correct: Excellent - Focus on edge cases and advanced topics
  • 19-22 correct: Strong - Review missed concepts
  • 15-18 correct: Good foundation - Need targeted study in weak areas
  • 10-14 correct: Moderate gaps - Focus on fundamentals and modern features
  • 0-9 correct: Significant gaps - Comprehensive curriculum needed

Topic Mapping

Based on wrong answers, you'll be assigned lessons:

  • Q1, 4, 21, 22, 24, 25: Lesson 01 - Type System & Coercion
  • Q3, 23: Lesson 02 - This Binding & Contexts
  • Q5, 6: Lesson 03 - Closures & Scope
  • Q7, 19, 20: Lesson 04 - Event Loop & Async Execution
  • Q8, 9: Lesson 05 - Promises & Async/Await
  • Q13, 14, 15: Lesson 06 - Prototypes & Classes
  • Q10, 11, 12: Lesson 07 - ES6+ Modern Features
  • Q16, 17, 18: Lesson 08 - Array Methods & Functional Programming