Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Classes and Object Lifecycle
What will be the output of this code?
class Cat:
    def __init__(self, name):
        self.name = name

my_cat = Cat('Whiskers')
print(my_cat.name)
AWhiskers
Bmy_cat
CCat
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand __init__ method usage

    The __init__ method sets the name attribute to 'Whiskers'.
  2. Step 2: Print the name attribute of my_cat

    Printing my_cat.name outputs 'Whiskers'.
  3. Final Answer:

    Whiskers -> Option A
  4. Quick Check:

    Object attribute access = 'Whiskers' [OK]
Quick Trick: Attributes set in __init__ are accessed via object.attribute [OK]
Common Mistakes:
  • Printing object instead of attribute
  • Forgetting to pass argument to constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes