Bird
0
0

Which of the following is the correct syntax to define a class named Book with a constructor that takes title as a parameter?

easy📝 Syntax Q3 of 15
Python - Classes and Object Lifecycle
Which of the following is the correct syntax to define a class named Book with a constructor that takes title as a parameter?
Aclass Book: def init(self, title): self.title = title
Bclass Book: def __init__(self, title): self.title = title
Cdef Book(title): self.title = title
Dclass Book(): def __init__(title): self.title = title
Step-by-Step Solution
Solution:
  1. Step 1: Check class and constructor syntax

    Python classes use class ClassName: and constructors use def __init__(self, ...):.
  2. Step 2: Verify parameter usage

    The first parameter must be self, then others like title. Inside, assign with self.title = title.
  3. Final Answer:

    class Book: def __init__(self, title): self.title = title -> Option B
  4. Quick Check:

    Constructor syntax = def __init__(self, param) [OK]
Quick Trick: Constructor must have 'self' as first parameter [OK]
Common Mistakes:
  • Omitting self parameter
  • Using def init instead of def __init__
  • Defining class with def instead of class keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes