0
0
Pythonprogramming~10 mins

__init__ method behavior 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 in a Python 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'
Aconstructor
Binit
C__init__
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'init' without underscores.
Using 'constructor' or other names instead of '__init__'.
2fill in blank
medium

Complete the code to create an instance of the class Car with make 'Toyota' and model 'Corolla'.

Python
my_car = Car([1], 'Corolla')
Drag options to blanks, or click blank then click option'
A'Toyota'
B'Honda'
C'Ford'
D'BMW'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong string for the make.
Forgetting to use quotes around the string.
3fill in blank
hard

Fix the error in the constructor method name to make the class work correctly.

Python
class Person:
    def [1](self, name):
        self.name = name
Drag options to blanks, or click blank then click option'
Ainitialize
B__init__
C_init_
Dinit__
Attempts:
3 left
💡 Hint
Common Mistakes
Using single underscores or misspelling the method name.
Using other names like 'initialize'.
4fill in blank
hard

Fill both blanks to complete the constructor that sets the attributes 'name' and 'age'.

Python
class Student:
    def __init__(self, name, age):
        self.[1] = name
        self.[2] = age
Drag options to blanks, or click blank then click option'
Aname
Bage
Cgrade
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing attribute names or using unrelated names like 'grade'.
Forgetting to use 'self.' before attribute names.
5fill in blank
hard

Fill all three blanks to complete the constructor that sets 'title', 'author', and 'pages' attributes.

Python
class Book:
    def __init__(self, title, author, pages):
        self.[1] = title
        self.[2] = author
        self.[3] = pages
Drag options to blanks, or click blank then click option'
Atitle
Bauthor
Cpages
Dpublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect attribute names like 'publisher'.
Forgetting 'self.' before attribute names.