Bird
0
0

Given this class:

hard📝 Application Q15 of 15
Python - Classes and Object Lifecycle
Given this class:
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

book1 = Book("1984", "Orwell")
book2 = Book("Animal Farm", "Orwell")

# Change author of book2 to "George Orwell"

Which code correctly updates book2 author without affecting book1?
Abook2["author"] = "George Orwell"
BBook.author = "George Orwell"
Cbook2.author = "George Orwell"
Dbook1.author = "George Orwell"
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance vs class attributes

    Changing book2.author modifies only that instance's attribute, not book1.
  2. Step 2: Avoid changing class attribute or other instance

    Assigning Book.author changes class attribute for all instances; changing book1.author affects wrong object; brackets are invalid syntax.
  3. Final Answer:

    book2.author = "George Orwell" -> Option C
  4. Quick Check:

    Set instance attribute with dot on correct object = book2.author = "George Orwell" [OK]
Quick Trick: Assign attribute on specific object with dot notation [OK]
Common Mistakes:
  • Changing class attribute instead of instance attribute
  • Modifying wrong object's attribute
  • Using brackets instead of dot notation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes