Bird
0
0

You want to create a class Rectangle that stores width and height, and has a method area returning the area. Which code correctly implements this?

hard📝 Application Q8 of 15
Python - Object-Oriented Programming Foundations
You want to create a class Rectangle that stores width and height, and has a method area returning the area. Which code correctly implements this?
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): width = width height = height def area(): return width * height
Cclass Rectangle: def __init__(self, w, h): self.w = w self.h = h def area(self): return w * h
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 __init__ method parameters and assignments

    class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height correctly uses self and assigns parameters to instance variables.
  2. Step 2: Verify area method uses self to access attributes

    class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height's area method uses self.width and self.height, which is correct.
  3. Final Answer:

    Option A correctly implements Rectangle with area method -> Option A
  4. Quick Check:

    Use self to access instance variables in methods [OK]
Quick Trick: Always use self to access attributes inside methods [OK]
Common Mistakes:
  • Omitting self in method parameters
  • Not using self to access attributes
  • Using wrong variable names inside methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes