Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Constructors and Object Initialization

What will be the output of this code?

class Cat:
    def __init__(self, name):
        self.name = name

c = Cat('Whiskers')
print(c.name)
ACat object
Bname
CError: name not defined
DWhiskers
Step-by-Step Solution
Solution:
  1. Step 1: Understand __init__ sets attribute

    The __init__ method assigns the string 'Whiskers' to self.name.
  2. Step 2: Print attribute value

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

    Whiskers -> Option D
  4. Quick Check:

    Attribute set in __init__ prints value [OK]
Quick Trick: Attributes set in __init__ print their assigned values [OK]
Common Mistakes:
MISTAKES
  • Expecting class name print
  • Confusing attribute with string 'name'
  • Thinking error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes