Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Python - Constructors and Object Initialization
What will this code print?
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person('Alice', 30)
print(p.age)
A30
BError
Cname
DAlice
Step-by-Step Solution
Solution:
  1. Step 1: Assign constructor parameters

    Constructor assigns 'Alice' to self.name and 30 to self.age.
  2. Step 2: Print age attribute

    Printing p.age outputs 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Attribute age = 30 [OK]
Quick Trick: Use self.attribute to access constructor parameters [OK]
Common Mistakes:
  • Printing wrong attribute
  • Confusing name and age
  • Expecting error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes