Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Constructors and Object Initialization
What will be the output of this code?
class Book:
    def __init__(self, title, pages=100):
        self.title = title
        self.pages = pages

book1 = Book('Python 101', 250)
book2 = Book('Learn AI')
print(book1.pages, book2.pages)
A100 250
B250 100
C250 250
DError: Missing argument for title
Step-by-Step Solution
Solution:
  1. Step 1: Understand argument passing

    book1 passes both title and pages, so pages=250. book2 passes only title, so pages uses default 100.
  2. Step 2: Print pages attributes

    book1.pages is 250, book2.pages is 100, so output is '250 100'.
  3. Final Answer:

    250 100 -> Option B
  4. Quick Check:

    Explicit args override defaults [OK]
Quick Trick: Explicit argument replaces default value [OK]
Common Mistakes:
  • Mixing order of arguments
  • Assuming default always used
  • Expecting error without all arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes