Bird
Raised Fist0

What will be the output of this code?

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

p = Person(30)
print(p.age + 5)
A30
B35
CError
Dp.age + 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute assignment in constructor

    p.age is set to 30 when object is created.
  2. Step 2: Calculate p.age + 5

    30 + 5 equals 35, which is printed.
  3. Final Answer:

    35 -> Option B
  4. Quick Check:

    Attribute value plus number = 35 [OK]
Quick Trick: Object attributes hold values accessible for calculations [OK]
Common Mistakes:
MISTAKES
  • Trying to add without attribute
  • Syntax errors in print statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes