Bird
Raised Fist0

Given the following code snippet, what will be the output?

medium📝 Analysis Q13 of Q15
LLD - Design — Parking Lot System
Given the following code snippet, what will be the output?
class Vehicle {
  String licensePlate;
  Vehicle(String plate) { licensePlate = plate; }
}
class Spot {
  Vehicle parkedVehicle;
  boolean isOccupied() { return parkedVehicle != null; }
}
Spot spot = new Spot();
System.out.println(spot.isOccupied());
Atrue
BCompilation error
Cnull
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Spot initialization

    The Spot object is created but parkedVehicle is not assigned, so it defaults to null.
  2. Step 2: Evaluate isOccupied method

    isOccupied returns true if parkedVehicle is not null; here it is null, so it returns false.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    parkedVehicle is null, so isOccupied() = false [OK]
Quick Trick: Unassigned vehicle means spot is free (false) [OK]
Common Mistakes:
MISTAKES
  • Assuming default boolean is true
  • Confusing null with false
  • Expecting compilation error due to missing constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes