Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q7 of 15
Python - Classes and Object Lifecycle
Identify the error in this code snippet:
class Book:
    def __init__(self, title):
        self.title = title

b = Book('1984')
print(Book.title)
AIncorrect class definition
BMissing self in __init__
CSyntax error in print statement
DAccessing instance attribute via class name
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute access

    Instance attributes belong to objects, not the class itself.
  2. Step 2: Check print statement

    Accessing Book.title tries to get a class attribute, but title is an instance attribute.
  3. Final Answer:

    Accessing instance attribute via class name -> Option D
  4. Quick Check:

    Instance attributes accessed via object, not class [OK]
Quick Trick: Use object.attribute to access instance attributes [OK]
Common Mistakes:
  • Accessing instance attributes through class name
  • Confusing class and instance attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes