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

dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.name)
print(dog2.name)
ABuddy Max
BMax Buddy
CBuddy Buddy
DMax Max
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance attribute assignment

    dog1.name is set to "Buddy" and dog2.name is set to "Max" separately.
  2. Step 2: Print instance attributes

    Printing dog1.name outputs "Buddy" and dog2.name outputs "Max".
  3. Final Answer:

    Buddy Max -> Option A
  4. Quick Check:

    Each object has its own name attribute [OK]
Quick Trick: Each object keeps its own attribute values [OK]
Common Mistakes:
  • Assuming all objects share the same attribute
  • Mixing up the order of print statements
  • Confusing class and instance attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes