Python object-oriented quiz

  • Admin
  • 1 min read

Eight questions on classes, objects, init, self, methods and inheritance.

Python

Python object-oriented quiz

8 questions, instant feedback

Question 1 of 8
Question 1 of 8 Which keyword defines a class?
Why this is the answer

class Dog: starts a class definition. The body is indented like a function body.

Question 2 of 8
Question 2 of 8 When does __init__ run?
Why this is the answer

__init__ is the initializer: it runs once each time you create an instance.

Question 3 of 8
Question 3 of 8 What does self refer to inside a method?
Why this is the answer

self is the particular object the method was called on, which is how methods reach that object's attributes.

Question 4 of 8
Question 4 of 8 How do you make Dog inherit from Animal?
Why this is the answer

The parent class goes in parentheses after the new class name. Java and C# use extends or a colon instead.

Question 5 of 8
Question 5 of 8 In a class, an attribute is…
Why this is the answer

Attributes hold an object's data, such as self.balance. Methods are the functions that work with it.

Question 6 of 8
Question 6 of 8 A method is…
Why this is the answer

Methods are functions attached to a class, which is why they can use self.

Question 7 of 8
Question 7 of 8 What does __str__ control?
Why this is the answer

__str__ returns the human-readable text used by print() and str().

Question 8 of 8
Question 8 of 8 Which line creates an object from a class called Task?
Why this is the answer

Calling the class like a function creates an instance and runs __init__.

Your answers stay in this browser. Nothing is sent to a server.