Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Class Methods and Static Methods
What will be the output of this code?
class Example:
    @classmethod
    def show(cls):
        return cls.__name__

print(Example.show())
AExample
Bshow
Ccls
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand what cls.__name__ returns

    cls refers to the class itself, and __name__ is the class name as a string.
  2. Step 2: Predict the output of Example.show()

    Calling Example.show() returns 'Example', the class name.
  3. Final Answer:

    Example -> Option A
  4. Quick Check:

    cls.__name__ returns class name string [OK]
Quick Trick: cls.__name__ gives the class name string [OK]
Common Mistakes:
MISTAKES
  • Expecting method name instead of class name
  • Confusing cls with instance
  • Thinking it causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes