Bird
0
0

You want to create a class Book that stores title and author. Which constructor correctly initializes these attributes and allows creating a Book object with both values?

hard📝 Application Q15 of 15
Python - Constructors and Object Initialization
You want to create a class Book that stores title and author. Which constructor correctly initializes these attributes and allows creating a Book object with both values?
Adef __init__(self, author): self.title = title self.author = author
Bdef __init__(self, title, author): self.title = title self.author = author
Cdef __init__(self, title): self.title = title self.author = author
Ddef __init__(self): title = '' author = ''
Step-by-Step Solution
Solution:
  1. Step 1: Check parameters needed

    Both title and author must be passed to the constructor to initialize attributes.
  2. Step 2: Verify attribute assignment

    Constructor must assign both self.title and self.author from parameters.
  3. Final Answer:

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

    Constructor with all attributes assigned = def __init__(self, title, author): self.title = title self.author = author [OK]
Quick Trick: Constructor needs all attributes as parameters and assigns them [OK]
Common Mistakes:
  • Missing parameters for all attributes
  • Assigning attributes without parameters
  • Using local variables instead of self attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes