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. In the Dependency Inversion Principle, who should own the interface: the high-level or the low-level module?

    The high-level module. It declares the interface in terms of what it needs, and low-level modules implement it. That is what inverts the dependency direction.

    AdvancedQuick answer1 min read
  2. Predict the output: a parent reference holds a child object that hides the parent's field. Which value do you get?

    Methods are chosen by the object's real type, but fields are chosen by the reference type. So p.name gives the parent's value while p.get() runs the child's method.

    AdvancedQuick answer1 min read
  3. What is a covariant return type in method overriding?

    An overriding method may return a more specific type (a subclass) than the parent's method declared. Callers using the child type then avoid casting.

    AdvancedQuick answer1 min read
  4. What is a leaky abstraction?

    A leaky abstraction promises to hide a detail but fails in some situations, so the user has to understand the hidden layer anyway. The simple view stops being enough.

    AdvancedQuick answer1 min read
  5. What is the difference between a Service Locator and Dependency Injection?

    With DI, dependencies are handed to the class from outside. With a Service Locator, the class asks a global registry for them. The locator hides what the class needs and makes testing harder.

    AdvancedQuick answer1 min read
  6. What is the method resolution order (MRO) in Python?

    MRO is the order in which Python searches a class and its parents when you call a method or read an attribute. Python builds it with the C3 rule, and you can see it in ClassName.__mro__.

    AdvancedQuick answer1 min read
  7. What rules must an overriding method follow to respect the Liskov Substitution Principle?

    It must accept at least what the parent accepts, promise at least what the parent promises, and throw no new kinds of failure. Weaker preconditions, stronger postconditions, same invariants.

    AdvancedQuick answer1 min read
  8. Why do APIs use static factory methods like List.of() instead of constructors?

    Static factory methods have names, can return cached or existing objects, can return a subtype, and can hide the implementation class. Constructors can do none of these.

    AdvancedQuick answer1 min read
  9. Your interface has twelve methods and most implementers leave several empty. How do you fix it without breaking everything?

    Introduce smaller role interfaces, make the big interface extend them, move clients to the small ones gradually, then retire the old one. Existing code keeps compiling throughout.

    AdvancedQuick answer1 min read