OOP & SOLID Interview Questions and Answers
Object-oriented design for any stack: classes and objects, encapsulation, inheritance and polymorphism, interfaces and abstract classes, composition over inheritance, SOLID with code examples, GRASP, value objects and immutability, code smells and refactoring, dependency injection, DDD aggregates and variance. Examples in TypeScript, Java and Python.
225 questions for OOP & SOLID interviews with answers and explanations - Junior, Middle and Senior levels. Sample questions, online quizzes and coding tasks.
- SOLID
- Polymorphism
- Composition
- DI
- Refactoring
Questions by level
Junior
Core concepts and syntax · 50 questions
Middle
Practice, nuances and optimization · 50 questions
Senior
Architecture, internals, edge cases · 50 questions
Sample questions
A few questions from each level with the correct answer and an explanation.
OOP & SOLID · Junior
- What is the difference between a class and an object?
Show the answer
Correct answer: A class is a blueprint, an object is a concrete instance built from it
A class describes the state and behavior that its instances will have. Each
new User()creates a separate object with its own field values. - Which four ideas are usually called the pillars of OOP?
Show the answer
Correct answer: Encapsulation, abstraction, inheritance, polymorphism
Encapsulation hides state behind an interface, abstraction models only what matters, inheritance reuses a type, and polymorphism lets one call work with many types.
- What is a constructor for?
Show the answer
Correct answer: To initialize a new object and put it into a valid state
The constructor runs once when the object is created. It sets fields and checks arguments so that the object is usable from the first moment.
- Which statement about encapsulation is correct?
Show the answer
Correct answer: It hides internal state and lets other code change it only through methods
Encapsulation protects the object's invariants: outside code can't put it into an invalid state because changes go through methods that check the rules.
OOP & SOLID · Middle
- According to LSP, how may a subclass change a method's preconditions and postconditions?
Show the answer
Correct answer: Preconditions can only be weakened, postconditions can only be strengthened
A subclass may accept more and promise more, never accept less or promise less. If the parent accepts any amount, a subclass that rejects amounts over 100 breaks callers.
- Which subclass change breaks LSP?
Show the answer
Correct answer:
withdraw(amount)in the parent accepts any positive amount, the subclass rejects amounts over 1000Strengthening a precondition means some calls that worked with the parent now fail. Adding methods or changing
toString()doesn't break existing callers. - Which return type is allowed when overriding
Animal create()in Java?Show the answer
Correct answer:
Dog create(), a covariant return typeReturn types may be narrowed (covariance): a caller expecting an
Animalis happy with aDog. Widening the return type would break callers. - What is the history rule of LSP?
Show the answer
Correct answer: A subtype must not allow state changes that the parent type forbids
If
ImmutablePointis the parent, aMutablePointsubclass breaks code that relied on the point never changing, even if every method signature matches.
OOP & SOLID · Senior
- Liskov and Wing define subtyping behaviorally. What does that add beyond matching method signatures?
Show the answer
Correct answer: Subtypes must keep the parent's invariants, contracts and allowed state changes
A type checker only sees signatures. Behavioral subtyping is about meaning: anything provable about the parent's objects must stay true for the subtype's objects.
- What does PECS mean for Java generics?
Show the answer
Correct answer: Producer
extends, Consumersuper: read from? extends T, write to? super TCollections.copy(List<? super T> dest, List<? extends T> src)readsTfrom the source and writesTinto the destination. That's covariance for reading and contravariance for writing. - Why must method parameter types be contravariant for a safe override?
Show the answer
Correct answer: Callers pass arguments valid for the parent, so the override must accept at least those
If
feed(food: Food)is overridden asfeed(food: Meat), a caller holding the parent type can passGrassand break the subclass. Accepting a wider type is always safe. - What is the expression problem?
Show the answer
Correct answer: Adding both new types and new operations without changing existing code is hard
Class hierarchies make new types cheap and new operations expensive. Unions with functions make operations cheap and types expensive. Visitors, type classes and multimethods try to get both.
Coding tasks
Write your solution right in the browser, and tests check it instantly. JavaScript interview classics (debounce, deepClone, Promise.all, an LRU cache), TypeScript tasks on generics, narrowing and utility types checked by the real compiler, Node.js backend logic (middleware, rate limiters, job queues), SQL queries in real PostgreSQL, and React components and hooks like a todo list, tabs, useDebounce and useInterval.
Open tasks →