Classes and Objects: Bundling Data with Behaviour · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Blueprints that build their own copies

Computing · Programming · ages 16-18
Name ______________________   Date ____________
  1. What is the usual job of __init__ in the Account class?

    • To print the balance whenever any attribute is read
    • To initialize the new account's starting attributes
    • To delete the account after its first method call
  2. Cat is a defined class whose initializer needs no additional arguments. Which line creates an instance?

    • pet_1 = Cat()
    • pet_1 = Cat
    • Cat = pet_1
  3. Two Cat instances each have an instance attribute name. Assigning pet_1.name = "Moss" must also change pet_2.name.

    Circle one:   True   False

  4. CoffeeOrder.loc is a class attribute. __init__ sets self.cup_size. No instance sets its own loc. Which statement fits this design?

    • loc is shared through the class; cup_size belongs to each order
    • loc belongs to each order; cup_size is shared through the class
    • Both loc and cup_size are shared through the class for every order
  5. Rectangle.area(self) returns self.length * self.width. room_1 has length 10 and width 15. What does room_1.area() return?

    • 25
    • 115
    • 150
  6. ProductionCar.update_max(self, speed) assigns self.max_mph = speed. Two separate cars start with max_mph = 0.0. car_1.update_max(250.0) runs. Which state follows?

    • car_1.max_mph = 250.0; car_2.max_mph = 250.0
    • car_1.max_mph = 0.0; car_2.max_mph = 250.0
    • car_1.max_mph = 250.0; car_2.max_mph = 0.0
  7. Separate balance variables and deposit/withdraw functions are reorganised as Account objects. What does this change provide by itself?

    • Each balance and its operations are grouped in one object
    • All account balances become one shared value on the class
    • Other code can no longer assign to any account attribute
  8. CoffeeOrder.cls_id starts at 10. Each initializer adds 1, then copies the counter into self.order_id. Three orders are created. What is the third order's order_id?

    • 12
    • 13
    • 10
LightMySky · lightmysky.comW1-mt_kRvg7LZ_kk-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Blueprints that build their own copies W1-mt_kRvg7LZ_kk-s1

  1. To initialize the new account's starting attributes · __init__ receives the new instance as self and assigns its starting balance.
  2. pet_1 = Cat() · Cat() calls the class to create an instance. Assigning Cat without parentheses only stores a reference to the class.
  3. False · This assignment changes pet_1's instance attribute. It does not assign a new value to pet_2.name.
  4. loc is shared through the class; cup_size belongs to each order · loc belongs to CoffeeOrder and is not shadowed here. self.cup_size is an attribute on the particular instance.
  5. 150 · The call passes room_1 as self. Its area calculation is 10 * 15 = 150.
  6. car_1.max_mph = 250.0; car_2.max_mph = 0.0 · The bound call passes car_1 as self and 250.0 as speed. Only car_1's max_mph attribute is assigned.
  7. Each balance and its operations are grouped in one object · A class groups related data and operations. It does not automatically prevent outside writes or merge all balances.
  8. 13 · The three initializers move the counter from 10 to 11, then 12, then 13. The third order stores 13.
Worksheet · LightMySky