Bird
0
0

Identify the bug in this Elevator class code:

medium📝 Analysis Q7 of 15
LLD - Design — Elevator System
Identify the bug in this Elevator class code:
class Elevator:
    def __init__(self):
        self.current_floor = 5
    def move_up(self):
        self.current_floor =+ 1

e = Elevator()
e.move_up()
print(e.current_floor)
AThe operator '=+' should be '+=' to increment current_floor
BThe constructor should initialize current_floor to 0
CThe method move_up should return current_floor
DThe print statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operator usage

    The operator '=+' is an assignment of positive 1, not an increment.
  2. Step 2: Correct operator for increment

    Use '+=' to increment the value of current_floor by 1.
  3. Final Answer:

    The operator '=+' should be '+=' to increment current_floor -> Option A
  4. Quick Check:

    Check operator usage for increment [OK]
Quick Trick: Use '+=' to increment variables, not '=+' [OK]
Common Mistakes:
MISTAKES
  • Confusing '=+' with '+=' operator
  • Not initializing variables properly
  • Ignoring method return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes