Answer key · 12 questions

Python Classes and Objects Exercises With Answers

All 12 questions for Python Classes and Objects in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Classes and Objects, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Which method initialises a new object?

  1. __main__
  2. __start__
  3. __init__correct
  4. init

Answer: __init__

Why: __init__ runs automatically when an instance is created.

02 · Multiple choice

What does self refer to inside a method?

  1. The class
  2. The module
  3. The current instancecorrect
  4. The parent class

Answer: The current instance

Why: self is the object on which the method was called.

03 · Multiple choice

How do you create an instance of Order?

  1. Order("latte", 4.5)correct
  2. Order.new(...)
  3. new Order(...)
  4. Order.init(...)

Answer: Order("latte", 4.5)

Why: Call the class like a function to create an instance.

04 · Multiple choice

What is an attribute?

  1. A function outside a class
  2. A file path
  3. Data stored on an objectcorrect
  4. A loop variable

Answer: Data stored on an object

Why: Attributes hold the state of a particular object.

05 · Fill in the blank

Complete the method name that initialises the object.

class Order:
    def ____(self, drink):
        self.drink = drink

Answer: __init__

Why: __init__ is the initialiser method.

06 · Fill in the blank

Complete the attribute assignment for the drink parameter.

class Order:
    def __init__(self, drink):
        self.____ = drink

Answer: drink

Why: self.drink stores the parameter on the instance.

07 · Fill in the blank

Complete the expression that multiplies price by quantity.

class Order:
    def total(self):
        return self.price ____ self.quantity

Answer: *

Why: self.price * self.quantity uses the object's own attributes.

08 · Fill in the blank

Complete the call that creates an Order instance.

order = ____("latte", 4.5)
print(order.price)

Answer: Order

Why: The class name followed by parentheses creates an instance.

09 · Predict output

What is printed?

class A:
    def __init__(self, x):
        self.x = x
print(A(3).x)

Answer: 3

Why: The instance stores 3 in its x attribute.

10 · Predict output

What is the output?

class A:
    def value(self):
        return 7
print(A().value())

Answer: 7

Why: The method returns 7.

11 · Predict output

What does this code print?

class Order:
    def __init__(self, price, quantity=2):
        self.price = price
        self.quantity = quantity
print(Order(4).price * Order(4).quantity)

Answer: 8

Why: Each Order gets price 4 and quantity 2, so the product is 8.

12 · Predict output

What does the program output?

class A:
    pass
print(A().__class__.__name__)

Answer: A

Why: The class name is A.

Try the questions first

Open the practice page for Python Classes and Objects and answer the same 12 questions with instant feedback.

Practice this lesson