Bird
0
0

What will be the output of this code?

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

d = Dog('Buddy')
print(d.name)
ABuddy
BDog
Cname
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and attribute assignment

    The Dog class has an __init__ method that sets self.name to the given argument.
  2. Step 2: Trace the code execution

    When d = Dog('Buddy') runs, d.name becomes 'Buddy'. Printing d.name outputs 'Buddy'.
  3. Final Answer:

    Buddy -> Option A
  4. Quick Check:

    Object attribute prints assigned value [OK]
Quick Trick: Print object.attribute to see stored value [OK]
Common Mistakes:
  • Printing class name instead of attribute
  • Expecting 'name' string output
  • Confusing attribute with variable name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes