Bird
Raised Fist0

What will this code print?

medium📝 Predict Output Q5 of Q15
Python - Object-Oriented Programming Foundations
What will this code print?
class Person:
    def __init__(self, name):
        self.name = name

p1 = Person('Alice')
p2 = Person('Bob')
print(p1.name, p2.name)
AError: name not found
BAlice Bob
Cname name
DPerson Person
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and attributes

    Two Person objects are created with names 'Alice' and 'Bob'.
  2. Step 2: Print their name attributes

    Printing p1.name and p2.name outputs 'Alice Bob'.
  3. Final Answer:

    Alice Bob -> Option B
  4. Quick Check:

    Multiple objects hold separate data [OK]
Quick Trick: Each object stores its own attribute values [OK]
Common Mistakes:
MISTAKES
  • Expecting class name instead of attribute values
  • Confusing attribute names with strings
  • Thinking it causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes