Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Python - Custom Exceptions
What will be the output of the following code?
class Book:
    pass

b = Book()
b.title = "Python 101"
print(b.title)
AAttributeError
BPython 101
CNone
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute addition to empty class instance

    The class Book is empty but Python allows adding attributes dynamically to instances.
  2. Step 2: Trace the code execution

    Instance b is created, then attribute title is added with value "Python 101". Printing b.title outputs the string.
  3. Final Answer:

    Python 101 -> Option B
  4. Quick Check:

    Dynamic attribute addition prints value = Python 101 [OK]
Quick Trick: You can add attributes to empty class instances anytime [OK]
Common Mistakes:
  • Expecting AttributeError for new attributes
  • Confusing class attributes with instance attributes
  • Thinking empty class disallows attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes