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 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 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