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 create an immutable class in Java?

    Make the class final, all fields private and final, set them only in the constructor, add no setters, and copy any mutable object on the way in and on the way out.

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

    Prefix the field with

    IntermediateQuick answer1 min read
  4. How do you make a Singleton thread-safe in Java?

    Use eager initialisation, the holder class idiom, or an enum. If you need lazy creation with double-checked locking, the field must be volatile.

    IntermediateQuick answer1 min read
  5. How do you spot a Single Responsibility violation in real code?

    Look for a class whose description needs the word and, imports from unrelated areas, unrelated groups of fields, or edits from different people for different reasons.

    IntermediateQuick answer1 min read
  6. How is the Interface Segregation Principle different from the Single Responsibility Principle?

    SRP is about a class having one reason to change. ISP is about the interface a client sees: it should be small and fit that client's needs. One is about implementation, the other about the contract.

    IntermediateQuick answer1 min read
  7. How would you build a simple factory in Python without a long if-elif chain?

    Keep a dictionary that maps names to classes and look the class up. Adding a new type is a one-line registration rather than another elif branch.

    IntermediateQuick answer1 min read
  8. 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
  9. What are the signs that code violates the Liskov Substitution Principle?

    Subclasses that throw UnsupportedOperationException, override a method to do nothing, or callers that must check instanceof before using a parent-typed object.

    IntermediateQuick answer1 min read
  10. What does an IoC container do, and when is it worth using?

    It creates objects, works out their dependencies and injects them for you, often also controlling their lifetime. It saves manual wiring in large applications, but is unnecessary in small ones.

    IntermediateQuick answer1 min read
  11. What happens in memory when you write new Student("Asha") in Java?

    new allocates memory for the object on the heap, sets fields to default values, runs the constructor, and returns a reference. The variable on the stack holds only that reference.

    IntermediateQuick answer1 min read
  12. What is dynamic method dispatch in Java?

    It is the way Java chooses which overridden method to run at runtime, based on the actual object type and not on the type of the reference variable.

    IntermediateQuick answer1 min read
  13. What is the difference between a shallow copy and a deep copy of an object?

    A shallow copy duplicates the object but shares the objects it refers to. A deep copy duplicates the whole graph, so the copy shares nothing with the original.

    IntermediateQuick answer1 min read
  14. What is the difference between Dependency Inversion, Dependency Injection and Inversion of Control?

    DIP is a design rule: depend on abstractions. DI is a technique: pass dependencies in from outside. IoC is the wider idea that a framework or caller controls the flow instead of your code.

    IntermediateQuick answer1 min read
  15. What is the difference between protected and default (package-private) access in Java?

    Default access lets only classes in the same package use the member. Protected allows the same package plus subclasses in any package, but through inheritance.

    IntermediateQuick answer1 min read
  16. What is the difference between the Factory Method and Abstract Factory patterns?

    Factory Method lets a subclass decide which single product to create. Abstract Factory creates a whole family of related products that are meant to be used together.

    IntermediateQuick answer1 min read
  17. What is the difference between the Observer pattern and Publish-Subscribe?

    In Observer, observers register directly with the subject and the subject calls them. In Pub-Sub, a message broker sits between publishers and subscribers, so neither knows the other.

    IntermediateQuick answer1 min read
  18. When does an object become eligible for garbage collection in Java?

    An object becomes eligible when no live thread can reach it through any chain of references. The JVM then frees its memory at a time of its own choosing.

    IntermediateQuick answer1 min read
  19. Why do many developers call Singleton an anti-pattern?

    It is global state in disguise: hidden dependencies, hard-to-reset state between tests, tight coupling to one concrete class, and trouble when you later need two instances.

    IntermediateQuick answer1 min read
  20. Why does Java not allow a class to extend two classes?

    To avoid the diamond problem: if two parents define the same method and a child inherits both, it is unclear which version should run. Java blocks it and lets you combine interfaces instead.

    IntermediateQuick answer1 min read
  21. Why does Square extending Rectangle violate the Liskov Substitution Principle?

    A Square must keep width and height equal, so setting one changes the other. Code written for any Rectangle, expecting independent sides, gets wrong results when given a Square.

    IntermediateQuick answer1 min read
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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