Bird
0
0

Find the bug in this code snippet:

medium📝 Analysis Q7 of 15
LLD - Design — Parking Lot System
Find the bug in this code snippet:
class Spot {
  boolean isAvailable = true;
  void park() {
    if (isAvailable = false) {
      System.out.println("Spot occupied");
    }
  }
}
AisAvailable should be initialized to false
BSystem.out.println syntax is incorrect
Cpark method should return boolean
DAssignment used instead of comparison in if condition
Step-by-Step Solution
Solution:
  1. Step 1: Analyze if condition

    Condition uses assignment (=) instead of comparison (==), causing logic error.
  2. Step 2: Correct usage

    Use 'if (isAvailable == false)' or '!isAvailable' to check availability.
  3. Final Answer:

    Assignment used instead of comparison in if condition -> Option D
  4. Quick Check:

    Use '==' for comparison, not '=' = B [OK]
Quick Trick: Use '==' to compare booleans, not '=' [OK]
Common Mistakes:
MISTAKES
  • Confusing assignment with comparison
  • Changing initial value unnecessarily
  • Incorrect print statement syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes