Bird
0
0

Identify the error in this Order class method:

medium📝 Analysis Q6 of 15
LLD - Design — Food Delivery System
Identify the error in this Order class method:
class Order {
  Map items = new HashMap<>();
  void add_item(String name, int qty) {
    items.put(name, qty);
  }
}
ANo error, code is correct
BMap should use List instead of Integer
CIt overwrites quantity instead of adding
DMethod name should be addItem
Step-by-Step Solution
Solution:
  1. Step 1: Understand add_item behavior

    Using put replaces existing quantity instead of adding to it.
  2. Step 2: Identify correct approach

    Should check if item exists and add qty to existing value.
  3. Final Answer:

    It overwrites quantity instead of adding -> Option C
  4. Quick Check:

    put() replaces values, not accumulates [OK]
Quick Trick: put() replaces; use get + add for quantities [OK]
Common Mistakes:
  • Assuming put adds quantities
  • Ignoring existing quantities
  • Confusing method naming with error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes