Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Python - Constructors and Object Initialization
What will this code print?
class Animal:
    def __init__(self, species='Dog'):
        self.species = species

animal1 = Animal()
animal2 = Animal('Cat')
print(animal1.species, animal2.species)
AError: missing argument
BCat Dog
CDog Dog
DDog Cat
Step-by-Step Solution
Solution:
  1. Step 1: Understand default parameter values

    If no argument is passed, species defaults to 'Dog'.
  2. Step 2: Analyze object creation and print

    animal1 uses default 'Dog', animal2 uses 'Cat'. Printing shows 'Dog Cat'.
  3. Final Answer:

    Dog Cat -> Option D
  4. Quick Check:

    Default parameter works = D [OK]
Quick Trick: Default parameters fill in missing arguments automatically [OK]
Common Mistakes:
  • Assuming missing argument causes error
  • Mixing order of printed species
  • Ignoring default parameter values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes