Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
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())
AError: missing self parameter
BBuddy says Woof!
CDog says Woof!
DWoof!
Step-by-Step Solution
Solution:
  1. Step 1: Understand the __init__ method

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

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

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

    Method uses self.name = Buddy [OK]
Quick Trick: Methods use self to access object data [OK]
Common Mistakes:
MISTAKES
  • Forgetting to pass 'self' in methods
  • Confusing class name with object name
  • Expecting method to print instead of return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes