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('Anna', 30)
print(p.age)
A30
BAnna
Cp.age
DError: missing argument
Step-by-Step Solution
Solution:
  1. Step 1: Analyze constructor parameters

    The constructor takes name and age and assigns them to the object.
  2. Step 2: Check the print statement

    Printing p.age outputs the age value, which is 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Accessing attribute age = 30 [OK]
Quick Trick: Attributes set in constructor accessed by object [OK]
Common Mistakes:
  • Printing name instead of age
  • Confusing attribute names
  • Not passing all required arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes