Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

JavaScript (ES2026) Module 11

Prototypes and classes interview questions

Interview questions on prototypes and classes: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

18 questions: 6 warm-ups and 12 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is the prototype chain in JavaScript?

    Answered in the lesson Prototypes and the prototype chain

  2. Fresher

    What does the new operator do?

    Answered in the lesson Constructor functions and the new operator

  3. Fresher

    What is the difference between a #private field and a property whose name starts with an underscore?

    Answered in the lesson Private fields, methods and brand checks

  4. Fresher to mid

    What does Symbol.species do, and which array methods use it?

    Answered in the lesson Extending built-ins: Array, Error and Map

  5. Fresher

    Why does Array.isArray exist when instanceof Array works?

    Answered in the lesson instanceof, duck typing and cross-realm checks

  6. Fresher

    What is the difference between composition and inheritance, and when would you choose each?

    Answered in the lesson Composition, mixins and OOP design in JavaScript

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A settings module creates each user's settings with Object.create(DEFAULTS) and then assigns only what the user changed. The web page shows every value correctly, but the mobile app, which reads the saved JSON, shows blank themes and font sizes for most users. Explain why, then fix both saving and loading.

    What it tests
    Own and inherited properties, what JSON.stringify, Object.keys and spread include, and putting the defaults back when data returns from JSON.
    First step
    Compare what the page reads with what JSON.stringify writes. A read walks the prototype chain, so settings.theme finds the default. JSON.stringify takes only the object's own enumerable properties, so every value still inherited from DEFAULTS is missing from the JSON, and the app finds nothing there.
    Follow-ups

    From the lesson Prototypes and the prototype chain

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  2. Mid to senior

    A shared utilities file adds Array.prototype.unique = function () { … } so that teams can write tags.unique(). In a code review you ask for it to be removed, and the author replies that it has worked for two years. What can it break today and in future releases, and how would you migrate away from it?

    What it tests
    How a built-in prototype is shared by all code, enumerability and for…in, clashes with libraries and future standard methods, and migrating a widely used helper safely.
    First step
    Ask who else sees the method: every array in the process, including the arrays inside third-party libraries. Then check how it was added. A plain assignment makes it enumerable, so every for…in loop over an array now visits unique, and a library or a future standard method with the same name collides with it.
    Follow-ups

    From the lesson Prototypes and the prototype chain

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  3. Fresher to mid

    A classic script on a shop page defines function Tracker(name) { this.name = name; this.events = []; }. One call site reads const t = Tracker('checkout'). That line throws nothing, but the next, t.events.push(e), throws a TypeError, and window.name is now 'checkout'. Explain both symptoms, fix the code and stop it happening again.

    What it tests
    How a plain call sets this in sloppy and strict code, spotting writes to the global object, and the guards, conventions and tools that prevent a forgotten new.
    First step
    Read the call as the engine does. Without new, Tracker('checkout') is a plain call, and in a sloppy classic script a plain call sets this to the global object, window. So this.name renamed the window and this.events created a global, and since the function returns nothing, t is undefined and t.events throws.
    Follow-ups

    From the lesson Constructor functions and the new operator

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to senior

    A data grid makes one Row object per record with a constructor that defines six methods inside its body, such as this.format = function () { … }. With 200,000 records, a heap snapshot shows about 1.2 million function objects, and the tab uses far more memory than the data needs. Explain why, estimate the cost, and fix it.

    What it tests
    Knowing that functions made in a constructor are new objects for each instance, estimating memory from a measured cost per object, and moving methods to a prototype without breaking callers.
    First step
    Count the functions. Each call of the constructor evaluates six function expressions, so 200,000 rows create 1,200,000 function objects, none of them shared. Then size them: in the lesson's run on Node.js 24.21.0 each such function cost about 80 bytes, so the methods alone take roughly 96 MB.
    Follow-ups

    From the lesson Constructor functions and the new operator

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  5. Mid to senior

    Your team wraps service objects in a Proxy that logs every method call. After a refactor moves the services' state into #private fields, every call through the proxy throws a TypeError, while calls on the unwrapped objects still work. Explain why, and design a logging wrapper that works with private fields.

    What it tests
    How private fields belong to the object itself, what this is inside a method called through a proxy, and designing wrappers that run methods on the real object.
    First step
    Trace one call. proxy.charge(100) reads charge through the proxy, gets the class's method and calls it with this set to the proxy. Inside, this.#balance looks for the private field on the proxy, which is a different object from the target and has no private fields, so the read throws a TypeError.
    Follow-ups

    From the lesson Private fields, methods and brand checks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  6. Mid to senior

    A payments library exposes Payment.isPayment(value), written as return value instanceof Payment. A security review says it can be fooled both ways. Show an object it wrongly accepts and a genuine payment it wrongly rejects, then rewrite the check. What can your version still not detect?

    What it tests
    The difference between a prototype check and a brand check, forging with Object.create, prototype changes and realms, and the limits of private-field brand checks.
    First step
    Recall what instanceof asks: is Payment.prototype on the object's prototype chain right now. Object.create(Payment.prototype) builds that chain without running the constructor, so it is accepted; Object.setPrototypeOf(payment, null), or a payment made in another realm, takes that prototype off the chain, so a real payment is rejected.
    Follow-ups

    From the lesson Private fields, methods and brand checks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  7. Mid to senior

    A SortedFares class extends Array and keeps fares ordered by amount in its push. fares.map((f) => f.km) returns a SortedFares that a later binary search trusts, and another bug shows the order broken after fares.reverse(). Explain both bugs, fix the class, and say whether it should extend Array at all.

    What it tests
    Symbol.species and how copying methods build their results, inherited mutators that bypass an override, class invariants, and choosing between extending a built-in and wrapping it.
    First step
    Follow how map builds its result. It reads fares.constructor[Symbol.species], which is SortedFares, calls new SortedFares(length) and stores each mapped value at its index without calling push. Then list what else changes an array without push: reverse, sort, splice, unshift, fill, copyWithin and assignment to an index or to length.
    Follow-ups

    From the lesson Extending built-ins: Array, Error and Map

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Mid to senior

    A ConfigMap extends Map, stores a #schema passed to its constructor and overrides set to validate each value. new ConfigMap(schema) works, but new ConfigMap(schema, savedEntries) throws a TypeError whose message is not the class's own. Explain the order of events, fix it, and name other ways values can bypass set.

    What it tests
    The order of construction in a subclass of a built-in, base constructors that call overridable methods, private fields before super() returns, and inherited methods that skip an override.
    First step
    Write down what super(savedEntries) does. Map's constructor creates the object, reads this.set, which is ConfigMap's override, and calls it for every entry. ConfigMap's fields are added only after super() returns, so set reads #schema from an object that does not have it yet, and the private read throws.
    Follow-ups

    From the lesson Extending built-ins: Array, Error and Map

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  9. Mid to senior

    A charting library validates its options with value instanceof Array and value instanceof Date. A customer embeds the chart in a same-origin iframe and passes options that the parent page created, and valid options are rejected. Explain why, and rewrite the validation so that it works for values from any realm.

    What it tests
    Realms and their separate built-ins, what instanceof compares, realm-independent checks for arrays, dates and plain objects, and when structured cloning removes the problem.
    First step
    Ask which realm made the values. The parent page created the arrays and dates, so their prototypes are the parent's Array.prototype and Date.prototype. The library runs in the iframe and compares with the iframe's Array and Date, whose prototypes never appear on those chains, so instanceof is false for valid options.
    Follow-ups

    From the lesson instanceof, duck typing and cross-realm checks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  10. Senior to staff

    After a dependency upgrade, an app's error handler stops recognising errors from an HTTP client: err instanceof HttpError is false, although the stack shows the client threw an HttpError. The lockfile shows two versions of the client installed. Explain the mechanism, the immediate fix, and how the library should make its errors recognisable.

    What it tests
    How two copies of a package create two classes, the dual package hazard, deduplicating dependencies, and type checks that do not depend on class identity.
    First step
    Find which copy each side uses. The app imports HttpError from one copy, while a dependency calls the other copy, which evaluates its own class with its own prototype. instanceof compares prototype objects, so an error from the second copy never matches the first copy's HttpError, whatever its name says.
    Follow-ups

    From the lesson instanceof, duck typing and cross-realm checks

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  11. Mid to senior

    A codebase has Notifier, SmsNotifier, RetryingSmsNotifier and QuietHoursRetryingSmsNotifier, and product now wants email and WhatsApp with the same retry and quiet-hours options. Propose a design that does not add a class per combination, show how you would migrate the existing callers safely, and explain how you would test it.

    What it tests
    Recognising combinatorial subclassing, composing collaborators and wrappers, dependency injection for testing, and migrating callers without a big-bang rewrite.
    First step
    Count the combinations first: one subclass per channel and set of options means channels times 2 to the power of options, 12 classes for three channels and two options, doubling with each new option. Then separate what varies: the channel, a sender with send and canReach, and each option, a wrapper around any sender.
    Follow-ups

    From the lesson Composition, mixins and OOP design in JavaScript

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  12. Mid to senior

    Two teams publish mixins for your Order class, and both define save(): Auditable writes an audit entry and Cached clears a cache. With Object.assign only one save runs. With subclass factories, the order is sometimes not saved at all, because one team's save never calls super.save(). Explain both behaviours and design mixins that combine safely.

    What it tests
    How Object.assign mixins overwrite, how subclass-factory mixins chain through super, the order of application, and when hooks or composed collaborators fit better than mixins.
    First step
    Look at what each technique builds. Object.assign copies properties onto Order.prototype, so the second mixin's save replaces the first, and both replace Order's own. Subclass factories build a chain, such as Cached(Auditable(BaseOrder)), and each save runs the next one only if it calls super.save().
    Follow-ups

    From the lesson Composition, mixins and OOP design in JavaScript

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All JavaScript (ES2026) interview questions · The JavaScript (ES2026) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.