Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Exercise 12 - Classes practice

5 total points

Directions

  • Due: 12/7/2025 @ 11:59 p.m.

  • Change the name of your notebook to EX12_FirstLast.ipynb, where First is your first name and Last is your last name.

  • For each of the following prompts, write or modify Python code that fulfills the requirements.

All problems are worth 0.5 points unless otherwise specified.

Problem 1

Create an instance of class P1 below and set it equal to a1. Replace None with your code.

class P1:
    
    def __init__(self):
        self.x = 1
    
a1 = None

type(a1)
NoneType

Problem 2

Create an instance of class P2 below and set the property of the instance named y equal to a2. Replace None with your code and add code outside of the class.

class P2:
    
    def __init__(self):
        
        self.y = 2
        

a2 = None

print(a2)
None

Problem 3

Complete the __init__ method in class P3 based on the how the code creates an instance of P3 in the outer scope. Set the argument passed in to __init__ to a property of P3 named ‘i’. Do not change the instance creation below. Replace pass and modify __init__ with your code.

class P3:
    
    def __init__(self):
        
        pass
        

a3 = P3(5)

print(a3.i)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 8
      3     def __init__(self):
      5         pass
----> 8 a3 = P3(5)
     10 print(a3.i)

TypeError: __init__() takes 1 positional argument but 2 were given

Problem 4

Complete the __init__ method in class P4 based on the how the code creates an instance of P4 in the outer scope. Set the arguments passed to init to two properties of P4 named k and m. Do not change the instance creation below. Replace pass and modify __init__ with your code.

class P4:
    
    def __init__(self):
        
        pass

a4 = P4(5, 6)

print(a4.k, a4.m)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 7
      3     def __init__(self):
      5         pass
----> 7 a4 = P4(5, 6)
      9 print(a4.k, a4.m)

TypeError: __init__() takes 1 positional argument but 3 were given

Problem 5

Create an instance of P5 based on the __init__ method.

The properties of P5 should be set as follows:

x should be 1

y should be 2

z should be 3

Make sure the instance creation is set equal to a variable named a5. Replace None with your code.

class P5:
    
    def __init__(self, x, y, z):
        
        self.x = x
        self.y = y
        self.z = z
        
a5 = None

print(a5.x, a5.y, a5.z)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], line 11
      7         self.z = z
      9 a5 = None
---> 11 print(a5.x, a5.y, a5.z)

AttributeError: 'NoneType' object has no attribute 'x'

Problem 6

Create a method in P6 named message that returns the string “correct!”. Replace pass with your own code.

class P6:

    pass
    
a6 = P6()

print(a6.message())
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 7
      3     pass
      5 a6 = P6()
----> 7 print(a6.message())

AttributeError: 'P6' object has no attribute 'message'

Problem 7

Complete the class P7 that has an __init__ function that sets two properties: x and y.

Add a method in P7 named multiply that multiplies the two instance properties x and y and returns the result. Replace pass with your own code.

Do not change the instance creation code in the outer scope below.

class P7:
    
    pass
    
a7 = P7(3.5, 2)

print(a7.multiply())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 5
      1 class P7:
      3     pass
----> 5 a7 = P7(3.5, 2)
      7 print(a7.multiply())

TypeError: P7() takes no arguments

Problem 8

Create a class named P8_child that inherits the methods and properties of P8_parent, and adds a new method named square to P8_child only that returns the result of the property x to the power of 2. Replace pass in P8_child with your own code.

Do not change the instance creation code in the outer scope.

class P8_parent:
    
    def __init__(self, x):
        
        self.x = x
        
class P8_child(P8_parent):
    
    pass
    
a8 = P8_child(5)

print(a8.square())
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 13
      9     pass
     11 a8 = P8_child(5)
---> 13 print(a8.square())

AttributeError: 'P8_child' object has no attribute 'square'

Problem 9

Create a class named P9_child that inherits the methods and properties of P9_parent, and add a new property to P9_child only that is named y. Create a method named add in P9_child only that adds x and y together and returns the result. Replace the pass with your code.

Do not change the instance creation code in the outer scope.

class P9_parent:
    
    def __init__(self, x):
        
        self.x = x
        
class P9_child(P9_parent):
    
    pass
    
a9 = P9_child(5, 4)

print(a9.add())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 11
      7 class P9_child(P9_parent):
      9     pass
---> 11 a9 = P9_child(5, 4)
     13 print(a9.add())

TypeError: __init__() takes 2 positional arguments but 3 were given

Problem 10

Finish the class named P10 that will print the property msg if the following code is executed.

Do not change the instance creation or print statement code in the outer scope.

class P10:
    
    def __init__(self):
        
        self.msg = "correct!"

    
a10 = P10()

print(a10)
<__main__.P10 object at 0x7f9b2c5524f0>