Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a constructor in Python?
A constructor is a special method named __init__ that runs automatically when you create a new object from a class. It sets up the object’s initial state.
Click to reveal answer
beginner
How do constructor parameters help when creating an object?
Constructor parameters let you give values to the object right when you create it. This way, each object can start with its own unique data.
Click to reveal answer
beginner
What does this code do?<br><pre>class Car:
def __init__(self, color):
self.color = color
my_car = Car('red')</pre>
This code defines a class Car with a constructor that takes a color parameter. When my_car is created, it sets its color to 'red'.
Click to reveal answer
intermediate
Can constructor parameters have default values? How?
Yes! You can give constructor parameters default values by assigning them in the __init__ method, like def __init__(self, color='blue'):. If no value is given, the default is used.
Click to reveal answer
beginner
Why do we use self in constructor parameters?
self represents the object being created. It lets us store the parameter values inside the object so they can be used later.
Click to reveal answer
What is the name of the constructor method in Python?
A__start__
B__init__
Cconstructor
Dinit
✗ Incorrect
The constructor method in Python is always named __init__.
What does the self parameter represent in a constructor?
AThe object being created
BThe class itself
CA global variable
DA function parameter unrelated to the object
✗ Incorrect
self refers to the specific object being created or used.
How can you give a constructor parameter a default value?
ABy calling a separate method
BBy setting it outside the class
CBy using a global variable
DBy assigning a value in the parameter list, e.g., <code>def __init__(self, x=5):</code>
✗ Incorrect
Default values are set by assigning them in the parameter list of the constructor.
What happens if you create an object without passing a required constructor parameter?
APython raises an error
BThe object is created with default values
CThe parameter is set to None automatically
DThe program ignores the missing parameter
✗ Incorrect
If a required parameter is missing, Python raises a TypeError.
Why do we assign constructor parameters to self attributes?
ATo make the parameters global
BTo delete the parameters after use
CTo save the values inside the object for later use
DTo print the values immediately
✗ Incorrect
Assigning to self stores the values inside the object so they can be accessed anytime.
Explain what constructor parameters are and why they are useful.
Think about how you give information to a new object when you create it.
You got /4 concepts.
Describe the role of self in a constructor and how it relates to parameters.
Consider how the object keeps its own information.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of constructor parameters in a Python class?
easy
A. To provide initial values to an object when it is created
B. To define methods that the class can use
C. To create global variables outside the class
D. To delete an object from memory
Solution
Step 1: Understand what constructor parameters do
Constructor parameters allow you to pass values to an object when you create it, so it starts with specific data.
Step 2: Compare options with this understanding
Only To provide initial values to an object when it is created describes this purpose correctly. Other options describe unrelated concepts.
Final Answer:
To provide initial values to an object when it is created -> Option A
Quick Check:
Constructor parameters = initial values [OK]
Hint: Constructor parameters set starting values for new objects [OK]
Common Mistakes:
Confusing constructor parameters with methods
Thinking constructor parameters delete objects
Believing constructor parameters create global variables
2. Which of the following is the correct way to define a constructor with parameters in Python?
easy
A. def __start__(self, name):
B. def constructor(name):
C. def init(self):
D. def __init__(self, name):
Solution
Step 1: Recall Python constructor syntax
Python uses the special method named __init__ with self as the first parameter to define constructors.
Step 2: Check each option
Only def __init__(self, name): uses the correct method name and includes self and a parameter.
Final Answer:
def __init__(self, name): -> Option D
Quick Check:
Constructor method = __init__ with self [OK]
Hint: Constructor method is always named __init__ with self first [OK]
Common Mistakes:
Using wrong method names like constructor or init
Omitting self parameter
Using incorrect special method names
3. What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} says Woof!"
my_dog = Dog("Buddy")
print(my_dog.speak())
medium
A. Buddy says Woof!
B. Woof!
C. Dog says Woof!
D. Error: missing parameter
Solution
Step 1: Understand constructor parameter usage
The constructor takes a name and assigns it to self.name. The object my_dog is created with name "Buddy".
Step 2: Analyze the speak method output
The speak method returns a string using self.name, so it returns "Buddy says Woof!".
Final Answer:
Buddy says Woof! -> Option A
Quick Check:
Object name used in speak = Buddy says Woof! [OK]
Hint: Constructor sets name; speak uses it to print message [OK]
Common Mistakes:
Ignoring the name parameter and expecting just 'Woof!'
Confusing class name with instance name
Assuming missing parameters cause error here
4. Identify the error in this class definition:
class Car:
def __init__(color):
self.color = color
my_car = Car("red")
medium
A. Missing quotes around "red"
B. Missing self parameter in __init__ method
C. Cannot assign to self.color
D. Wrong method name, should be __start__
Solution
Step 1: Check __init__ method parameters
The first parameter of __init__ must be self to refer to the instance. Here, self is missing.
Step 2: Confirm other parts are correct
Method name __init__ is correct, assignment to self.color is valid, and "red" is properly quoted.
Final Answer:
Missing self parameter in __init__ method -> Option B
Quick Check:
__init__ first parameter = self [OK]
Hint: Always include self as first parameter in __init__ [OK]
Common Mistakes:
Forgetting self parameter
Changing __init__ method name
Misunderstanding self usage
5. You want to create a class Book that stores title and author when you create a new book object. Which constructor correctly sets these attributes?
hard
A. def __init__(title, author):
self.title = title
self.author = author
B. def __init__(self):
title = title
author = author
C. def __init__(self, title, author):
self.title = title
self.author = author
D. def __init__(self, title, author):
title = self.title
author = self.author
Solution
Step 1: Check parameter list and self usage
The constructor must have self as first parameter, then title and author. def __init__(self, title, author):
self.title = title
self.author = author has this correct.
Step 2: Verify attribute assignment
Attributes must be assigned as self.title = title and self.author = author to store values in the object. def __init__(self, title, author):
self.title = title
self.author = author does this correctly.
Final Answer:
def __init__(self, title, author):
self.title = title
self.author = author -> Option C
Quick Check:
Use self to assign attributes in __init__ [OK]
Hint: Use self.attribute = parameter inside __init__ [OK]
Common Mistakes:
Omitting self parameter
Assigning parameters to themselves instead of self