Bird
0
0

Identify the error in this code snippet:

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

kitty = Cat("black")
print(kitty[color])
AUsing brackets instead of dot to access attribute
BMissing parentheses in class definition
CIncorrect constructor name
DAttribute 'color' not defined
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute access syntax

    The code uses kitty[color], which tries to access like a dictionary key, but color is an attribute, not a key.
  2. Step 2: Correct syntax for attribute access

    Use dot notation: kitty.color to access the attribute.
  3. Final Answer:

    Using brackets instead of dot to access attribute -> Option A
  4. Quick Check:

    Attributes use dot, not brackets = Using brackets instead of dot to access attribute [OK]
Quick Trick: Use dot, not brackets, to access attributes [OK]
Common Mistakes:
  • Using brackets like kitty[color]
  • Thinking attributes are dictionary keys
  • Confusing attribute access with indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes