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, age=30):
        self.age = age

p1 = Person()
p2 = Person(25)
print(p1.age, p2.age)
A25 30
BError: missing argument
C30 30
D30 25
Step-by-Step Solution
Solution:
  1. Step 1: Check default parameter usage

    p1 is created without argument, so age defaults to 30. p2 is created with 25.
  2. Step 2: Print ages

    Printing p1.age and p2.age outputs '30 25'.
  3. Final Answer:

    30 25 -> Option D
  4. Quick Check:

    Default parameter used when no argument given [OK]
Quick Trick: Default __init__ parameters fill missing arguments [OK]
Common Mistakes:
  • Assuming error on missing argument
  • Swapping printed values
  • Ignoring default parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes