Practice
1. In a parking system, should
Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state likelicensePlateand provide default behavior, it requires fields and method implementations.Step 2: Abstract class vs interface capabilities
Abstract classes can have instance fields and concrete methods, while interfaces cannot hold instance state (only constants) and default methods cannot manage instance variables.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces lack instance fields [check]
Common Mistakes:
2. Given a
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state like speed and provide default behavior likeaccelerate(), it requires fields and method implementations.Step 2: Choose between abstract class and interface
Java interfaces cannot hold instance state (fields) prior to Java 8 default methods, and even then, no instance variables. Abstract classes can hold state and provide default method implementations, making them suitable here.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs fields -> abstract class; interfaces lack instance variables [check]
Common Mistakes:
3. A class has a private constructor and a static synchronized
getInstance() method returning a single instance. Which OOP concept or pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed safely across threads.Step 2: Eliminate other creational patterns
Factory Method allows multiple instances, Builder focuses on stepwise construction, and Prototype clones objects; none enforce single instance with thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static method -> Singleton pattern [check]
Hint: Private constructor + static synchronized method -> Singleton [check]
Common Mistakes:
4. The Meeting Scheduler needs to support ElectricCar attendees who require ChargingSpots. What is the minimum change to the current Java design to add this feature without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclass ElectricCar extends Vehicle allows extension without changing existing class code.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP to include new mappings supports new vehicle and spot types without class changes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + update map -> no existing class modified [check]
Hint: Extend enums + subclass + map update -> no class changes [check]
Common Mistakes:
5. The system needs to support
ElectricCar vehicles and ChargingSpot parking spots without modifying existing classes. What is the minimum change to the current Java design to support this extensibility?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility map controls which vehicle types fit which spots; updating it supports new ElectricCar and ChargingSpot types without class changes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> no existing class changes [check]
Hint: Extend enums + subclasses + compatibility map, no class edits [check]
Common Mistakes:
