Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Encapsulation and Data Protection
What will be the output of this code?
class Cat():
    def __init__(self, name):
        self.name = name

kitty = Cat('Mittens')
print(kitty.name)
Akitty
Bname
CMittens
DError: Attribute not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute assignment in constructor

    The constructor assigns the public attribute name with the value passed ('Mittens').
  2. Step 2: Access and print the attribute

    Printing kitty.name outputs the string 'Mittens'.
  3. Final Answer:

    Mittens -> Option C
  4. Quick Check:

    Public attribute value = printed correctly [OK]
Quick Trick: Public attributes print their assigned values directly [OK]
Common Mistakes:
MISTAKES
  • Expecting variable name instead of value
  • Confusing object name with attribute
  • Assuming attribute is private and inaccessible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes