Section19.12Multiple Choice Questions for Multiple Classes
Checkpoint19.12.1.
Q-1: Given the Point class below. What are the object methods in the Point class? Pick all that apply.
class Point:
def __init__(self, initX, initY):
""" Create a new point at the given coordinates. """
self.x = initX
self.y = initY
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return f"x = {self.x}, y = {self.y}"
def halfway(self, target):
mx = (self.x + target.x) / 2
my = (self.y + target.y) / 2
return Point(mx, my)
self.x
This stores the x position for the point. It is an object attribute.
self.distanceFromOrigin
This is an object method in the Point class
self.y
This stores the y position for the point. It is an object attribute.
self.halfway
This is an object method in the Point class
Checkpoint19.12.2.
Q-2: Given the code below, what are the object attribute(s) in the Rectangle class? Pick all that apply.
class Rectangle:
def __init__(self, p1, p2):
""" Create a new rectangle with the given points """
self.p1 = p1
self.p2 = p2
def area(self):
""" Return the area of the rectangle """
width = abs(self.p1.x - self.p2.x)
height = abs(self.p1.y - self.p2.y)
return width * height
self.p1
This is an object attribute in the Rectangle class.
self.__init__
This is an object method in the Rectangle class.
self.p2
This is an object attribute in the Rectangle class.
self.area
This is an object method in the Rectangle class.
Checkpoint19.12.3.
Q-3: Given the following code, what is breeds?
class Dog:
breeds = ["Bulldog", "Poodle", "Chihuahua", "Dachshund"]
def __init__(self, name, breed):
self.name = name
if breed not in Dog.breeds:
Dog.breeds.append(breed)
self.breed_index = Dog.breeds.index(breed)
def speak(self):
return "bark"
class attribute
Correct! It is a class attribute (only exists in the class).
class method
It is not a class method.
object attribute
It is not an object attribute.
object method
It is not an object method.
Checkpoint19.12.4.
Q-4: Given the following code, what is speak?
class Dog:
breeds = ["Bulldog", "Poodle", "Chihuahua", "Dachshund"]
def __init__(self, name, breed):
self.name = name
if breed not in Dog.breeds:
Dog.breeds.append(breed)
self.breed_index = Dog.breeds.index(breed)
def speak(self):
return "bark"
class attribute
It is not a class attribute.
class method
It is not a class method.
object attribute
It is not an object attribute.
object method
Correct! It is an object method.
Checkpoint19.12.5.
Q-5: Given the following code, what is self.breed_index?
class Dog:
breeds = ["Bulldog", "Poodle", "Chihuahua", "Dachshund"]
def __init__(self, name, breed):
self.name = name
if breed not in Dog.breeds:
Dog.breeds.append(breed)
self.breed_index = Dog.breeds.index(breed)
def speak(self):
return "bark"