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

my_dog = Dog('Buddy')
print(my_dog.bark())
Amy_dog says Woof!
BWoof!
CBuddy says Woof!
DError: missing self parameter
Step-by-Step Solution
Solution:
  1. Step 1: Understand the class and method

    The Dog class stores the dog's name and the bark method returns a string with the dog's name.
  2. Step 2: Trace the code execution

    Creating my_dog = Dog('Buddy') sets self.name to 'Buddy'. Calling my_dog.bark() 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!'
  • Printing variable name instead of value
  • Confusing method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes