Bclass Book:
def __init__(self, title, author):
title = title
author = author
b = Book('1984', 'Orwell')
print(b.title, b.author)
Cclass Book:
def __init__(self, title, author):
self.title = title
b = Book('1984', 'Orwell')
print(b.title, b.author)
Dclass Book:
def __init__(self, title, author):
self.title = title
self.author = author
b = Book('1984', 'Orwell')
print(b.title, b.author)
Step-by-Step Solution
Solution:
Step 1: Check attribute assignment in constructor
The correct code defines __init__ with title and author parameters and assigns self.title = title and self.author = author.
Step 2: Verify object creation and printing
The object is created by passing arguments Book('1984', 'Orwell') matching the parameters, and both attributes print correctly. Others fail on missing self, incomplete assignments, or parameter mismatch.
Final Answer:
Correctly assigns both attributes to self using parameters -> Option D
Quick Check:
Assign all attributes with self and pass parameters [OK]
Quick Trick:Assign all attributes with self and pass parameters [OK]
Common Mistakes:
Not assigning attributes to self
Missing parameters in constructor
Providing arguments to a parameterless constructor
Master "Classes and Object Lifecycle" in Python
9 interactive learning modes - each teaches the same concept differently