Liskov Substitution Principle (LSP)
Subclasses must be substitutable for their base class without breaking the program — no surprise exceptions or weakened contracts.
Questions in this quiz
10 questions on Liskov Substitution Principle (LSP). Work through them here, then start the quiz to answer them and see your score.
1.The Liskov Substitution Principle states that:
- AAll subclasses must implement all interfaces
- BSubclasses must be smaller in code size than the superclass
- CSubclasses must override every method from the superclass
- DObjects of a subclass must be substitutable for objects of the superclass without breaking the program's correctness
2.Why does Square extending Rectangle violate LSP?
- AThe area formula is different for squares
- BRectangle has independent setWidth()/setHeight(); Square must keep width=height, breaking the postcondition "setting width doesn't change height"
- CSquares and rectangles are mathematically unrelated
- DSquare method names differ from Rectangle
3.Which subclass violates LSP?
// Base class class Bird { public void fly() { /* flaps wings */ } } // Option A class Eagle extends Bird { @Override public void fly() { /* soars gracefully */ } } // Option B class Penguin extends Bird { @Override public void fly() { throw new UnsupportedOperationException("Penguins can't fly"); } }- AOption B — Penguin throws an exception that callers of Bird.fly() don't expect
- BNeither violates LSP
- COption A — Eagle changes the flying behaviour
- DBoth violate LSP
4.Which of these is a sign that LSP is being violated?
- ASubclasses are used via the superclass type in calling code
- BA subclass adds additional public methods
- CAll subclasses implement all interface methods
- DA subclass overrides a method by throwing UnsupportedOperationException or returning an empty/null value the superclass never returns
5.How does LSP relate to "program to interfaces, not implementations"?
- AThey are unrelated principles
- BBoth focus on contracts/behaviour: programming to an interface means depending on the contract; LSP ensures every implementation truly honours that contract so any can be safely swapped
- CLSP only applies to abstract classes, not interfaces
- DProgramming to interfaces automatically guarantees LSP
6.What is the "contract" of a method that LSP requires subclasses to honour?
- AThe method's exact line count and performance
- BThe method's visibility (public/protected/private)
- CPreconditions (what callers must provide), postconditions (what the method guarantees to return/change), and invariants (what must always be true)
- DThe exact same implementation logic
7.A ReadOnlyList extends ArrayList and overrides add() to throw UnsupportedOperationException. Does this violate LSP?
- AOnly if add() is called directly, not through a List reference
- BNo — subclasses can always restrict behaviour
- CNo — the Java standard library does this in Collections.unmodifiableList()
- DYes — code expecting ArrayList.add() to succeed will fail when given a ReadOnlyList
8.What does "strengthening preconditions" mean, and why does it violate LSP?
- AAdding more validation is always fine and doesn't affect substitutability
- BAdding null checks that the superclass didn't have
- CA subclass requiring MORE from callers than the superclass does — callers that satisfy the superclass contract may now fail with the subclass
- DMaking the method run faster with additional checks
9.A Vehicle superclass has a method refuel(). A Tesla extends Vehicle and overrides refuel() to throw "EVs don't use fuel". LSP violation?
- AYes — code calling vehicle.refuel() will break when the vehicle is a Tesla
- BNo — EVs are a valid type of vehicle and can override any method
- COnly a violation if refuel() is called from outside the Vehicle class
- DNo — throwing an informative exception is always acceptable
10.What is the practical impact of LSP violations in a large codebase?
- AMinor readability issues
- BOnly performance degradation
- CUnexpected runtime exceptions, incorrect behaviour when swapping implementations, and code that must type-check before calling methods
- DNo impact — type systems catch all LSP violations at compile time