Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Object-Oriented Programming Foundations
What will be the output of this code?
class Dog:
    def __init__(self, name):
        self.name = name
    def speak(self):
        return self.name + ' says Woof!'

my_dog = Dog('Buddy')
print(my_dog.speak())
ABuddy
BWoof! says Buddy
CBuddy says Woof!
DError: speak() missing self argument
Step-by-Step Solution
Solution:
  1. Step 1: Understand class and method behavior

    The Dog class stores a name and the speak method returns the name plus ' says Woof!'.
  2. Step 2: Trace the code execution

    Creating my_dog with name 'Buddy' and calling speak() returns 'Buddy says Woof!'.
  3. Final Answer:

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

    Method returns name + ' says Woof!' = A [OK]
Quick Trick: Method returns name plus message string [OK]
Common Mistakes:
  • Mixing order of words in output
  • Forgetting to pass self in method
  • Expecting error due to method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes