Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- What is the Observer pattern? Explain with a cricket score example.
A subject keeps a list of observers and tells all of them when its state changes. Observers subscribe and unsubscribe at runtime, and the subject does not need to know what they do with the news.
BeginnerQuick answer1 min read - What is the Singleton pattern and how do you implement it?
Singleton ensures a class has exactly one instance and gives a global access point to it. Make the constructor private, keep the instance in a static field, and return it from a static method.
BeginnerQuick answer1 min read - Why use a factory instead of calling new directly?
A factory hides which concrete class is created and how. Callers ask for what they need, and creation rules can change in one place without touching the callers.
BeginnerQuick answer1 min read - 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 - 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 - 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 - 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 - 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