quiz 6 Flashcards

1
Q

Every Python class requires:

A

a constructor method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is it impossible to create a static variable in a Python class.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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())

A

“Scar”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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”

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A

“Kind”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does inheritance mean in object-oriented programming?

A

Ability of a class to derive members of parent class as a part of its definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Python you can derive a class from two parent classes but not from three parent classes.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

This generates an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To define an object x in Python that inherits its attributes and methods from a parent class y, we use x = subclass(y).

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would one define the function of the “-“ (minus) operator for a new class?

A

define a __sub__() method on the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly