Interview Q&A / Object-Oriented Programming

INTERVIEW Q&A · 69 QUESTIONS

Object-Oriented Programming interview questions

The four pillars, interfaces, abstract classes and design basics.

  1. How can polymorphism replace a long if-else or switch chain?

    Give each case its own class with the same method, then call that method on the object. Adding a new case means adding a class, not editing the old, tested chain.

    IntermediateQuick answer1 min read
  2. How do you make a property private in JavaScript classes?

    Prefix the field with

    IntermediateQuick answer1 min read
  3. Show the Open/Closed Principle in JavaScript with a list of validation rules.

    Keep rules as a list of small functions and run them all with one loop. A new rule is added by appending a function, and the validating code is never changed.

    IntermediateQuick answer1 min read
  4. Why must you unsubscribe observers, and how do you design for it?

    A subject holds strong references to its observers, so a forgotten one stays in memory and keeps receiving events. Return an unsubscribe function from subscribe() and call it when done.

    IntermediateQuick answer1 min read