quiz 6 Flashcards
Every Python class requires:
a constructor method
Is it impossible to create a static variable in a Python class.
False
What is the output of the following code?
class Lion():
def __init__(self, x=”Scar”):
self.x = x
def roar(self): print(self.x)
new_lion = Lion()
print(new_lion.roar())
“Scar”
How many instance variables will exist after you run the following code?
class Person():
def __init__(self, weight = 160):
self.weight = weight
next_person = Person(150)
next_person.foot_size = 10
next_person.eyes = “green”
4
What is the output of the following:
class Tiger():
def __init__(self, x):
self.x = x
self.change(self.x)
def change(self, y): y = "Evil"
new_tiger = Tiger(“Kind”)
print(new_tiger.x)
“Kind”
What does inheritance mean in object-oriented programming?
Ability of a class to derive members of parent class as a part of its definition
In Python you can derive a class from two parent classes but not from three parent classes.
False
What is the output of the following code?
class Big_Cat:
def __init__(self):
self.x = “dangerous”
class Cat(Big_Cat):
def __init__(self):
self.y = “quiet”
new_cat = Cat()
print(new_cat.x, new_cat.y)
This generates an error.
To define an object x in Python that inherits its attributes and methods from a parent class y, we use x = subclass(y).
False
How would one define the function of the “-“ (minus) operator for a new class?
define a __sub__() method on the class.