Bird
0
0

What will be the output of this code?

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

e1 = Employee('E123')
e2 = Employee('E456')
print(e1.id, e2.id)
AE456 E123
BE123 E456
CE123 E123
DError: id attribute not found
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the __init__ method

    The constructor assigns the parameter 'id' to the instance attribute 'self.id'.
  2. Step 2: Check object creation

    e1 gets 'E123' and e2 gets 'E456' as their respective 'id' attributes.
  3. Step 3: Print statement

    Printing e1.id and e2.id outputs 'E123 E456'.
  4. Final Answer:

    E123 E456 -> Option B
  5. Quick Check:

    Each object has its own 'id' attribute value [OK]
Quick Trick: Instance attributes hold values per object [OK]
Common Mistakes:
  • Assuming both objects share the same attribute value
  • Confusing instance attributes with class attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes