Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Methods and Behavior Definition
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())
AWoof!
BBuddy says Woof!
Cmy_dog says Woof!
DError: missing self parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and method call

    The object my_dog is created with name 'Buddy'. Calling bark() uses self.name which is 'Buddy'.
  2. Step 2: Evaluate the return value

    The method returns the string "Buddy says Woof!" which is printed.
  3. Final Answer:

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

    Method uses self.name = Buddy [OK]
Quick Trick: Instance methods use self to access object data [OK]
Common Mistakes:
  • Ignoring self and expecting just 'Woof!'
  • Confusing variable name with object name
  • Assuming method returns nothing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes