Bird
Raised Fist0

Find the error in this class definition and choose the correct fix:

medium📝 Debug Q14 of Q15
Python - Object-Oriented Programming Foundations
Find the error in this class definition and choose the correct fix:
class Book:
    def __init__(title, author):
        self.title = title
        self.author = author
AAdd 'self' as the first parameter in __init__ method.
BChange __init__ to init without underscores.
CRemove self from inside the method.
DRename class to book (lowercase).
Step-by-Step Solution
Solution:
  1. Step 1: Identify the __init__ method parameters

    The first parameter of any instance method must be self to refer to the object.
  2. Step 2: Check the given code

    The __init__ method lacks self as the first parameter, causing an error when assigning attributes.
  3. Final Answer:

    Add 'self' as the first parameter in __init__ method. -> Option A
  4. Quick Check:

    Instance methods need self first [OK]
Quick Trick: Always put self as first method parameter [OK]
Common Mistakes:
MISTAKES
  • Forgetting self in method parameters
  • Changing __init__ name incorrectly
  • Ignoring case sensitivity in class names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes