Bird
0
0

You want to create a class Rectangle that stores width and height and has a method area returning the area. Which is the correct complete class definition?

hard📝 Application Q8 of 15
Python - Classes and Object Lifecycle
You want to create a class Rectangle that stores width and height and has a method area returning the area. Which is the correct complete class definition?
Aclass Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height
Bclass Rectangle: def __init__(width, height): self.width = width self.height = height def area(): return width * height
Cclass Rectangle: def __init__(self, w, h): width = w height = h def area(self): return width * height
Dclass Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(): return self.width * self.height
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters and assignments

    Constructor must have self and assign parameters to self.width and self.height.
  2. Step 2: Check method area definition

    Method area must have self and return self.width * self.height.
  3. Final Answer:

    Complete class with proper self usage in constructor and method -> Option A
  4. Quick Check:

    Use self for attributes and methods [OK]
Quick Trick: Use self everywhere inside class methods [OK]
Common Mistakes:
  • Omitting self in methods
  • Assigning parameters to local variables only
  • Using parameters directly without self

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes