Bird
0
0

Given the following code snippet, what will be the total cost stored in the Order after checkout?

medium📝 Analysis Q13 of 15
LLD - Design — Online Shopping Cart
Given the following code snippet, what will be the total cost stored in the Order after checkout?
product1 = Product(id=1, name='Pen', price=2)
product2 = Product(id=2, name='Notebook', price=5)
cart = Cart()
cart.addProduct(product1, 3)
cart.addProduct(product2, 2)
order = Order(cart)
order.checkout()
A16
B19
C10
D7
Step-by-Step Solution
Solution:
  1. Step 1: Calculate total cost from cart products and quantities

    Pen price = 2, quantity = 3 -> 2 * 3 = 6
    Notebook price = 5, quantity = 2 -> 5 * 2 = 10
    Total = 6 + 10 = 16
  2. Step 2: Check if any additional charges or taxes apply

    No extra charges mentioned, so total cost should be 16.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    2*3 + 5*2 = 16 [OK]
Quick Trick: Multiply price by quantity, then sum all products [OK]
Common Mistakes:
  • Adding quantities instead of multiplying by price
  • Mixing product prices and quantities incorrectly
  • Ignoring one product's cost

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes