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, color):
        self.color = color

c = Cat('black')
print(c.color)
Acolor
Bblack
CCat
DError
Step-by-Step Solution
Solution:
  1. Step 1: Trace object creation and attribute assignment

    The object c is created with color 'black', so c.color is set to 'black'.
  2. Step 2: Understand print output

    Printing c.color outputs the string 'black'.
  3. Final Answer:

    black -> Option B
  4. Quick Check:

    Instance attribute value printed = 'black' [OK]
Quick Trick: Print instance attribute with object.attribute [OK]
Common Mistakes:
  • Printing the attribute name instead of its value
  • Confusing class name with attribute
  • Expecting an error due to misunderstanding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes