Bird
0
0

You want to create a class Book that stores title and author when you create a new book object. Which constructor correctly sets these attributes?

hard📝 Application Q15 of 15
Python - Constructors and Object Initialization
You want to create a class Book that stores title and author when you create a new book object. Which constructor correctly sets these attributes?
Adef __init__(title, author): self.title = title self.author = author
Bdef __init__(self): title = title author = author
Cdef __init__(self, title, author): self.title = title self.author = author
Ddef __init__(self, title, author): title = self.title author = self.author
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter list and self usage

    The constructor must have self as first parameter, then title and author. def __init__(self, title, author): self.title = title self.author = author has this correct.
  2. Step 2: Verify attribute assignment

    Attributes must be assigned as self.title = title and self.author = author to store values in the object. def __init__(self, title, author): self.title = title self.author = author does this correctly.
  3. Final Answer:

    def __init__(self, title, author): self.title = title self.author = author -> Option C
  4. Quick Check:

    Use self to assign attributes in __init__ [OK]
Quick Trick: Use self.attribute = parameter inside __init__ [OK]
Common Mistakes:
  • Omitting self parameter
  • Assigning parameters to themselves instead of self
  • Reversing assignment order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes