PrepPilotPractise free

OOP & Design Patterns interview questions

SOLID, composition, common patterns and clean code

17 questions · SOLID · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is encapsulation and why is it important?

Encapsulation is the practice of bundling data and methods that operate on that data within a class, and restricting access to some of the object's components. This protects the internal state and prevents unintended interference, making code safer and easier to maintain.

Can you explain inheritance with a simple example?

Inheritance allows a class to inherit properties and methods from another class. For example, a Car class can inherit from a Vehicle class, gaining its speed and acceleration methods, while adding its own features like honk.

What is polymorphism in OOP?

Polymorphism lets you use a single interface to represent different types. For instance, you can call draw() on a list of shapes, and each shape (circle, rectangle) will implement its own version of draw().

What is abstraction and how does it help?

Abstraction hides complex implementation details and exposes only what’s necessary. It helps by simplifying interfaces, so users don’t need to understand the inner workings to use a class or method.

Intermediate questions

Why is composition often preferred over inheritance?

Composition is preferred because it allows you to build classes by combining behaviors, rather than relying on rigid hierarchies. This makes code more flexible and easier to change, as you can swap components without affecting unrelated parts.

Give an example of the Single Responsibility Principle.

A class that only handles printing invoices, like InvoicePrinter, follows the Single Responsibility Principle. It doesn’t manage invoice data or calculations, just the printing logic, making it easier to maintain and test.

How does the Open/Closed Principle improve code?

The Open/Closed Principle means classes are open for extension but closed for modification. You can add new functionality by extending classes, rather than changing existing code, which reduces the risk of breaking features.

What are the drawbacks of the Singleton pattern?

Singletons can introduce global state, making code harder to test and maintain. They can also create hidden dependencies and issues in multi-threaded environments if not implemented carefully.

How does the Factory pattern help with object creation?

The Factory pattern centralizes object creation, allowing you to instantiate different types based on input or configuration. This decouples client code from concrete classes and makes it easier to add new types.

What is the purpose of the Adapter pattern?

The Adapter pattern allows incompatible interfaces to work together by wrapping one interface with another. This is useful when integrating third-party libraries or legacy code with your system.

Describe the Decorator pattern with a use case.

The Decorator pattern lets you add behavior to objects dynamically. For example, you can wrap a data repository with a logging decorator to record operations, without modifying the original repository class.

How does dependency injection improve testability?

Dependency injection allows you to pass mock or stub dependencies into a class, making it easier to test in isolation. You don’t have to rely on real implementations, which can be slow or difficult to set up.

Senior questions

How would you design a class model for a parking lot system?

See the answer in the app, free

How would you refactor a class with too many responsibilities?

See the answer in the app, free

How do you decide between using a Strategy pattern or inheritance?

See the answer in the app, free

How would you implement Observer in a library management system?

See the answer in the app, free

What code smells do you look for during a code review?

See the answer in the app, free

More Developer fundamentals topics