OOPs practice questions with answers
Practise OOPs questions at your own pace: encapsulation, inheritance, polymorphism, SOLID and design patterns. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
What are the four pillars of object-oriented programming?
easy
- Encapsulation, abstraction, inheritance, polymorphism
- Classes, objects, methods, fields
- Compilation, linking, loading, execution
- Variables, loops, functions, modules
Answer: A. Encapsulation, abstraction, inheritance, polymorphism
Explanation: Encapsulation hides internal state, abstraction exposes only what matters, inheritance reuses behaviour, polymorphism lets one interface have many forms.
What is encapsulation?
easy
- Inheriting from many classes
- Keeping data and the methods that use it together, and hiding internal details behind an interface
- Overloading operators
- Writing code in capsules
Answer: B. Keeping data and the methods that use it together, and hiding internal details behind an interface
Explanation: Private fields with public methods let a class protect its invariants and change its internals without breaking callers.
Method overloading is:
easy
- A subclass redefining a parent's method
- Several methods with the same name but different parameters in one class
- Calling a method too often
- A runtime error
Answer: B. Several methods with the same name but different parameters in one class
Explanation: Overloading is resolved at compile time. Overriding (a subclass redefining a method) is resolved at run time.
What is a constructor?
easy
- A method that destroys objects
- A special method that initialises a new object
- A static variable
- A type of loop
Answer: B. A special method that initialises a new object
Explanation: It runs when an object is created and sets up its initial state.
What does "favour composition over inheritance" mean?
medium
- Never use inheritance
- Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a), for looser coupling
- Use only interfaces
- Put all code in one class
Answer: B. Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a), for looser coupling
Explanation: Composition lets you swap parts at run time and avoids fragile base classes.
Which SOLID principle says a class should have only one reason to change?
medium
- Open/closed
- Single responsibility
- Liskov substitution
- Dependency inversion
Answer: B. Single responsibility
Explanation: SRP: one responsibility per class. Open/closed: extend without modifying. Liskov: subtypes must be usable wherever the base type is.
A Square subclass of Rectangle breaks code that sets width and height independently. Which principle is violated?
medium
- Single responsibility
- Liskov substitution
- Interface segregation
- DRY
Answer: B. Liskov substitution
Explanation: Code written for Rectangle expects width and height to change separately; Square cannot honour that, so it is not a true substitute.
What is the diamond problem?
medium
- A memory leak
- Ambiguity when a class inherits the same member from two parents that share a base class
- A sorting bug
- Recursive constructors
Answer: B. Ambiguity when a class inherits the same member from two parents that share a base class
Explanation: C++ solves it with virtual inheritance, Python with its method resolution order, and Java avoids it by allowing only one superclass.
Which design pattern makes sure a class has exactly one instance?
hard
- Factory
- Singleton
- Observer
- Strategy
Answer: B. Singleton
Explanation: Singletons are easy to misuse: they are global state and make testing harder. Dependency injection is often a better choice.
Which pattern notifies a list of subscribers when an object's state changes?
hard
- Adapter
- Observer
- Decorator
- Builder
Answer: B. Observer
Explanation: Event listeners, pub/sub systems and reactive UIs are all built on the observer idea.
What is dependency inversion?
hard
- Subclasses depend on parents
- High-level code depends on abstractions (interfaces), not concrete classes, so implementations can be swapped
- Removing all dependencies
- Calling methods in reverse order
Answer: B. High-level code depends on abstractions (interfaces), not concrete classes, so implementations can be swapped
Explanation: For example, a service takes a PaymentGateway interface, so tests can pass a fake and production a real gateway.
Which are examples of runtime polymorphism? (Select all that apply)
hard
- Overriding a method in a subclass and calling it through a base reference
- Implementing an interface method in several classes
- Overloading a method with different parameter types
- Calling a virtual function in C++ through a pointer
Answer: A. Overriding a method in a subclass and calling it through a base reference; B. Implementing an interface method in several classes; D. Calling a virtual function in C++ through a pointer
Explanation: Overloading is decided at compile time (static polymorphism). The others choose the implementation from the actual object at run time.