Bird
0
0

Examine the following class snippet:

medium📝 Analysis Q6 of 15
LLD - Design — Parking Lot System
Examine the following class snippet:
class ParkingFloor {
  List spots;
  void addSpot(Spot spot) {
    spots.add(spot);
  }
}

What is the primary issue with this class design?
AThe method addSpot should return a boolean indicating success.
BThe 'spots' list is not initialized before adding elements.
CThe class should not have a method to add spots dynamically.
DThe Spot parameter should be of type Vehicle instead.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the class definition

    The class ParkingFloor has a List named spots but no constructor or initialization shown.
  2. Step 2: Identify the problem

    Without initializing 'spots' (e.g., new ArrayList<>()), calling spots.add(spot) will cause a NullPointerException.
  3. Final Answer:

    The 'spots' list is not initialized before adding elements. -> Option B
  4. Quick Check:

    Check if 'spots' is initialized before use [OK]
Quick Trick: Always initialize collections before use [OK]
Common Mistakes:
MISTAKES
  • Assuming the list is auto-initialized
  • Confusing parameter types
  • Expecting addSpot to return a value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes