Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Encapsulation and Data Protection
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)
ADog
BError
Cmy_dog
DBuddy
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute assignment

    The constructor sets self.name to the value passed, which is 'Buddy'.
  2. Step 2: Print the attribute value

    Printing my_dog.name outputs 'Buddy'.
  3. Final Answer:

    Buddy -> Option D
  4. Quick Check:

    Print attribute value = Buddy [OK]
Quick Trick: Print object.attribute to see stored value [OK]
Common Mistakes:
MISTAKES
  • Expecting class name instead of attribute value
  • Confusing object name with attribute
  • Thinking it causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes