Bird
0
0

What will be the output of this Python code?

medium📝 Predict Output Q5 of 15
Python - Object-Oriented Programming Foundations
What will be the output of this Python code?
class Animal:
    def __init__(self, species):
        self.species = species

cat = Animal('Cat')
print(cat.species)
AAnimal
Bspecies
CCat
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the class and object

    The class Animal has an __init__ method that sets self.species to the passed argument.
  2. Step 2: Check the object instantiation

    cat = Animal('Cat') creates an object with species attribute set to 'Cat'.
  3. Step 3: Understand the print statement

    print(cat.species) outputs the value of the species attribute, which is 'Cat'.
  4. Final Answer:

    Cat -> Option C
  5. Quick Check:

    Object attribute prints assigned value [OK]
Quick Trick: Object attributes print assigned values [OK]
Common Mistakes:
  • Printing the attribute name instead of its value
  • Confusing class name with attribute
  • Expecting an error due to misunderstanding __init__

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes