What does the Single Responsibility Principle (SRP) mean in real code?
SRP means each class or module should have only one reason to change. It should do one job well, like a chef focusing only on baking bread, not cooking the whole meal.
Click to reveal answer
intermediate
Explain the Open/Closed Principle (OCP) with a simple example.
OCP means code should be open for extension but closed for modification. For example, adding new payment methods without changing existing payment code, like adding new toppings to a pizza without changing the base recipe.
Click to reveal answer
intermediate
What is the Liskov Substitution Principle (LSP) in practice?
LSP means subclasses should work anywhere their parent class is expected without errors. Like replacing a car with a sports car that still drives the same way without surprises.
Click to reveal answer
advanced
How does the Interface Segregation Principle (ISP) improve code design?
ISP says clients should not depend on interfaces they don't use. It’s like giving a remote control with only the buttons you need, not a giant one with confusing extras.
Click to reveal answer
advanced
Describe the Dependency Inversion Principle (DIP) with a real-life analogy.
DIP means high-level modules should not depend on low-level modules directly. Instead, both depend on abstractions. Like using a universal plug adapter so your device works anywhere without changing the device itself.
Click to reveal answer
Which SOLID principle suggests that a class should have only one reason to change?
ASingle Responsibility Principle
BOpen/Closed Principle
CLiskov Substitution Principle
DDependency Inversion Principle
✗ Incorrect
The Single Responsibility Principle states that a class should have only one reason to change.
What does the Open/Closed Principle encourage?
AExtending code without modifying existing code
BModifying existing code frequently
CUsing only abstract classes
DAvoiding inheritance
✗ Incorrect
OCP encourages extending code behavior without changing existing code.
According to Liskov Substitution Principle, a subclass should:
AChange the behavior of the parent class
BDepend on concrete classes
CWork anywhere the parent class is expected
DHave fewer methods than the parent
✗ Incorrect
LSP requires subclasses to be substitutable for their parent classes without errors.
Interface Segregation Principle advises to:
AUse one big interface for all clients
BSplit interfaces so clients only use what they need
CAvoid interfaces altogether
DMake interfaces dependent on concrete classes
✗ Incorrect
ISP suggests splitting interfaces to avoid forcing clients to depend on unused methods.
Dependency Inversion Principle promotes:
ANo dependencies at all
BLow-level modules depend on high-level modules
CHigh-level modules depend on low-level modules
DBoth depend on abstractions
✗ Incorrect
DIP states both high-level and low-level modules should depend on abstractions.
Explain how you would apply the Single Responsibility Principle in a simple user management system.
Think about dividing tasks like storing user info and checking passwords.
You got /3 concepts.
Describe how the Dependency Inversion Principle helps in making code easier to test and maintain.
Consider how using interfaces allows replacing parts without breaking the whole system.
You got /3 concepts.
Practice
(1/5)
1. Which SOLID principle encourages a class to have only one reason to change?
easy
A. Dependency Inversion Principle
B. Open/Closed Principle
C. Liskov Substitution Principle
D. Single Responsibility Principle
Solution
Step 1: Understand the principle names
Single Responsibility Principle means one class should do one job only.
Step 2: Match principle to description
Only Single Responsibility Principle talks about one reason to change.
Final Answer:
Single Responsibility Principle -> Option D
Quick Check:
One class, one reason to change = Single Responsibility Principle [OK]
Hint: One class, one job means Single Responsibility Principle [OK]
Common Mistakes:
Confusing Single Responsibility with Open/Closed
Thinking Dependency Inversion means single responsibility
Mixing Liskov Substitution with class responsibilities
2. Which of the following code snippets correctly applies the Dependency Inversion Principle?
easy
A. class Car { Engine engine = new Engine(); }
B. interface Engine { void start(); } class Car { Engine engine; Car(Engine e) { engine = e; } }
C. class Car { void start() { Engine engine = new Engine(); engine.start(); } }
D. class Engine { void start() {} } class Car extends Engine {}
Solution
Step 1: Identify Dependency Inversion Principle
It says depend on abstractions (interfaces), not concrete classes.
Step 2: Check which code uses interfaces
interface Engine { void start(); } class Car { Engine engine; Car(Engine e) { engine = e; } } uses interface Engine and injects it into Car, following DIP.
Final Answer:
Interface with injection to depend on abstraction -> Option B
Quick Check:
Depend on interface, not concrete class = DIP [OK]
Hint: Look for interface usage and constructor injection [OK]
Extending concrete classes instead of using interfaces
Not using constructor or setter injection
3. Given this code snippet, what will be the output?
interface Shape { double area(); }
class Rectangle implements Shape {
double width, height;
Rectangle(double w, double h) { width = w; height = h; }
public double area() { return width * height; }
}
class Circle implements Shape {
double radius;
Circle(double r) { radius = r; }
public double area() { return 3.14 * radius * radius; }
}
class AreaCalculator {
double totalArea(Shape[] shapes) {
double total = 0;
for (Shape s : shapes) total += s.area();
return total;
}
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Rectangle(2,3), new Circle(1) };
System.out.println(new AreaCalculator().totalArea(shapes));
}
}
medium
A. 9.14
B. 12.14
C. 15.42
D. Error at runtime
Solution
Step 1: Calculate Rectangle area
Rectangle area = 2 * 3 = 6
Step 2: Calculate Circle area
Circle area = 3.14 * 1 * 1 = 3.14
Step 3: Sum areas
Total area = 6 + 3.14 = 9.14
Final Answer:
9.14 -> Option A
Quick Check:
6 + 3.14 = 9.14 [OK]
Hint: Add areas of shapes using interface method calls [OK]
Common Mistakes:
Using wrong formula for circle area
Adding radius instead of area
Forgetting to sum both shapes
4. Identify the violation of the Open/Closed Principle in this code:
class Report {
void printReport(String type) {
if (type.equals("PDF")) { System.out.println("Printing PDF"); }
else if (type.equals("HTML")) { System.out.println("Printing HTML"); }
}
}
medium
A. Adding new report types requires changing printReport method
B. Using if-else is always correct for Open/Closed Principle
C. The class is closed for modification
D. The method uses polymorphism correctly
Solution
Step 1: Understand Open/Closed Principle
Classes should be open for extension but closed for modification.
Step 2: Analyze printReport method
Adding new types means editing if-else, so modifying existing code.
Final Answer:
Adding new report types requires changing printReport method -> Option A
Quick Check:
Modifying code to add features breaks Open/Closed Principle [OK]
Hint: If adding features needs code change, Open/Closed is violated [OK]
Common Mistakes:
Thinking if-else is always fine
Confusing Open/Closed with Single Responsibility
Ignoring need for polymorphism
5. You have a payment system with classes for CreditCardPayment and PayPalPayment. To follow the Interface Segregation Principle, how should you design the interfaces?
hard
A. Create one large interface Payment with all methods for all payment types
B. Make CreditCardPayment inherit from PayPalPayment
C. Create separate interfaces like CreditCardPaymentInterface and PayPalPaymentInterface with only relevant methods
D. Use a single abstract class with all payment methods implemented