Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Methods and Behavior Definition
What will be the output of this code?
class Bird:
    def __init__(self, species):
        self.species = species
    def sing(self):
        return f"{self.species} sings beautifully!"

b = Bird('Canary')
print(b.sing())
ABird sings beautifully!
BCanary sings beautifully!
CError: missing self parameter
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance attribute assignment

    The constructor assigns species to self.species.
  2. Step 2: Method returns formatted string using self.species

    The sing method returns the string with the species name.
  3. Final Answer:

    Canary sings beautifully! -> Option B
  4. Quick Check:

    Instance methods access instance attributes via self [OK]
Quick Trick: Instance methods use self to access attributes [OK]
Common Mistakes:
  • Confusing class name with instance attribute
  • Forgetting to call method with parentheses
  • Incorrect string formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes