Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Object-Oriented Programming Foundations
What will be the output of this code?
class Book:
    def __init__(self, title):
        self.title = title

b = Book('Python 101')
print(b.title)
APython 101
BBook
Ctitle
DError: title not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand the __init__ method

    The __init__ method sets the object's title attribute to 'Python 101'.
  2. Step 2: Check the print statement

    Printing b.title outputs the string 'Python 101'.
  3. Final Answer:

    Python 101 -> Option A
  4. Quick Check:

    Object attribute access = 'Python 101' [OK]
Quick Trick: Object attributes accessed with dot notation print values [OK]
Common Mistakes:
  • Expecting class name instead of attribute value
  • Confusing attribute name with string output
  • Thinking it causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes