Bird
Raised Fist0

What will be the output of this code?

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

What will be the output of this code?

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

my_dog = Dog("Buddy")
print(my_dog.name)
ABuddy
Bname
CDog
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and attribute assignment

    The __init__ method sets self.name to "Buddy" when my_dog is created.
  2. Step 2: Check the print statement

    Printing my_dog.name outputs the string "Buddy" stored in the attribute.
  3. Final Answer:

    Buddy -> Option A
  4. Quick Check:

    Object attribute value = Buddy [OK]
Quick Trick: Print object.attribute to see stored value from __init__ [OK]
Common Mistakes:
MISTAKES
  • Printing class name instead of attribute
  • Expecting attribute name instead of value
  • Assuming code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes