0
0
Pythonprogramming~10 mins

Object initialization flow in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the constructor method for a class.

Python
class Car:
    def [1](self, make, model):
        self.make = make
        self.model = model
Drag options to blanks, or click blank then click option'
Astart
Binit
Cconstructor
D__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'init' without underscores.
Using 'constructor' which is not a Python method name.
2fill in blank
medium

Complete the code to create an object of the class Car.

Python
my_car = Car([1], 'Model S')
Drag options to blanks, or click blank then click option'
A'Tesla'
BTesla
CCar
D"Model S"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around string arguments.
Passing the class name instead of a string.
3fill in blank
hard

Fix the error in the method to correctly initialize the attribute.

Python
class Person:
    def __init__(self, name):
        self.[1] = name

p = Person('Alice')
print(p.name)
Drag options to blanks, or click blank then click option'
AName
Bnam
Cname
Dnames
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized or misspelled attribute names.
Mismatch between attribute and print statement.
4fill in blank
hard

Fill both blanks to create a class with an attribute and a method that returns it.

Python
class Book:
    def __init__(self, title):
        self.[1] = title
    def get_[2](self):
        return self.title
Drag options to blanks, or click blank then click option'
Atitle
Bname
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for attribute and method.
Mismatch between attribute and method names.
5fill in blank
hard

Fill all three blanks to initialize an object with two attributes and a method that returns a formatted string.

Python
class Student:
    def __init__(self, name, age):
        self.[1] = name
        self.[2] = age
    def info(self):
        return f"Name: {self.[3], Age: {self.age}"
Drag options to blanks, or click blank then click option'
Aname
Bage
Dstudent
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for attributes and in the method.
Forgetting to initialize both attributes.