Bird
0
0

Why does the following code cause an error?

medium📝 Analysis Q7 of 15
LLD - Design — Online Shopping Cart
Why does the following code cause an error?
order = Order()
order.totalPrice = cart.totalPrice
order.checkout()

Assuming totalPrice is a method in Cart.
Acheckout method must be called before setting totalPrice
BOrder class cannot access Cart properties directly
CtotalPrice is a private attribute and cannot be accessed
DtotalPrice should be called as a method with parentheses
Step-by-Step Solution
Solution:
  1. Step 1: Identify totalPrice usage

    totalPrice is a method, so it must be called with parentheses to get the value.
  2. Step 2: Explain error cause

    Assigning method reference instead of calling it causes an error or unexpected behavior.
  3. Final Answer:

    totalPrice should be called as a method with parentheses -> Option D
  4. Quick Check:

    Method call needs parentheses [OK]
Quick Trick: Call methods with () to get return value [OK]
Common Mistakes:
  • Assigning method reference instead of result
  • Assuming direct property access
  • Wrong order of method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes