Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Python - Class Methods and Static Methods
What will be the output of the following code?
class Dog:
    species = 'Canine'

    @classmethod
    def get_species(cls):
        return cls.species

print(Dog.get_species())
A'Canine'
BNone
C'Dog'
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand class attribute access via cls

    The class method get_species returns cls.species, which is 'Canine'.
  2. Step 2: Check the print statement output

    Calling Dog.get_species() returns 'Canine', which is printed.
  3. Final Answer:

    'Canine' -> Option A
  4. Quick Check:

    cls.species = 'Canine' [OK]
Quick Trick: Class methods access class variables via cls [OK]
Common Mistakes:
  • Expecting instance name instead of class attribute
  • Confusing output with class name string
  • Thinking it returns None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes