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 job of __init__()?

    • to print the object
    • to set the starting values of a new object
    • to delete an object
  2. With class Cat defined, which line creates an instance of it?

    • pet_1 = Cat()
    • Cat = pet_1()
    • def Cat():
  3. pet_1 and pet_2, both built from class Cat, share one set of attribute values.

    Circle one:   True   False

  4. In class CoffeeOrder, loc = "Cafe Coffee" sits in the class body while cup_size is set in __init__. Which statement is right?

    • loc is shared by all orders while cup_size is per order
    • both belong to each single order
    • loc is per order while cup_size is shared
  5. room_1 = Rectangle() gets length 10 and width 15. What does room_1.area() return?

    • 25
    • 115
    • 150
  6. car_1 = ProductionCar("McLaren", "Speedtail", 2020) then car_1.update_max(250.0). What changed?

    • every car built from the class now has 250.0
    • the class forgot the make and model
    • only car_1 holds 250.0 as its top speed
  7. A balance in a plain variable plus free deposit and withdraw functions becomes a class Account. What improved?

    • balance and its two operations live together, and each account guards its own state
    • the program runs without any variables
    • withdraw no longer needs the balance value
  8. Two orders are built in a row and each constructor raises CoffeeOrder.cls_id by 1. What order_id does the second order get?

    • 1
    • 2
    • 0
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 set the starting values of a new object · It runs at creation time and fills in the fresh object state.
  2. pet_1 = Cat() · Calling the class name builds one new object of that class.
  3. False · Each instance carries its own data, so changing one leaves the other alone.
  4. loc is shared by all orders while cup_size is per order · Class body data is shared, while self data in the constructor is per object.
  5. 150 · The method multiplies that object values: 10 times 15 is 150.
  6. only car_1 holds 250.0 as its top speed · Inside update_max, self means car_1, so only that object is updated.
  7. balance and its two operations live together, and each account guards its own state · Bundling keeps related code in one place and stops stray lines rewriting the balance.
  8. 2 · The shared counter moves once per creation, so the second object takes the next number.
Worksheet · LightMySky