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
    def bark(self):
        return f"{self.name} says Woof!"

my_dog = Dog("Buddy")
print(my_dog.bark())
ADog says Woof!
BWoof!
CBuddy says Woof!
DError: missing self parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand the __init__ method

    The __init__ method sets self.name to "Buddy" when my_dog is created.
  2. Step 2: Analyze the bark method output

    The bark method returns a string using self.name, so it returns "Buddy says Woof!".
  3. Final Answer:

    Buddy says Woof! -> Option C
  4. Quick Check:

    Method uses self.name = Buddy [OK]
Quick Trick: Methods use self to access object data [OK]
Common Mistakes:
  • Ignoring self and expecting just 'Woof!'
  • Confusing class name with instance name
  • Forgetting to pass name argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes