Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Classes and Object Lifecycle
What will be the output of this code?
class Person:
    def __init__(self, name):
        self.name = name

p = Person('Alice')
print(p.name)
APerson object
BAlice
Cname
DError: name not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and attribute assignment

    The constructor sets self.name to 'Alice' when p is created.
  2. Step 2: Print the attribute value

    Printing p.name outputs the string 'Alice'.
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    Accessing attribute = prints value [OK]
Quick Trick: Object attributes accessed with dot notation [OK]
Common Mistakes:
  • Expecting object type printout
  • Confusing attribute name with string
  • Forgetting to use self in constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes