Logo
Flashcard >runestone Inheritance - Foundations of Python Programming
Add Flashcard
1/7

After you run the code, new_cat = Cheshire("Cat1"), how many instance variables exist for the new_cat instance of Cheshire?

A. 1
B. 2
C. 3
D. 4

D. 4

✔️ Neither Cheshire nor Cat defines an init constructor method, so the grandaprent class, Pet, will have it's init method called. That constructor method sets the instance variables name, hunger, boredom, and sounds.

profile imgrajana posted a year ago

After you run the code, new_cat = Cheshire("Cat1"), how many instance variables exist for the new_cat instance of Cheshire?

A. 1
B. 2
C. 3
D. 4

D. 4

✔️ Neither Cheshire nor Cat defines an init constructor method, so the grandaprent class, Pet, will have it's init method called. That constructor method sets the instance variables name, hunger, boredom, and sounds.

What would print after running the following code:

new_cat = Cheshire("Cat1”)
class Siamese(Cat):
  def song(self):
    print("I am a purrrfect creature.")
another_cat = Siamese("Cat2")
another_cat.song()

A. I am a purrrfect creature.
B. Error
C. Pumpkin
D. Nothing. There’s no print statement.

A. I am a purrrfect creature.

✔️ another_cat is an instance of Siamese, so its song() method is invoked.

What would print after running the following code:

new_cat = Cheshire("Cat1”)
class Siamese(Cat):
  def song(self):
    print("I am a purrrfect creature.")
another_cat = Siamese("Cat2")
new_cat.song()

A. We are Siamese if you please. We are Siamese if you don’t please.
B. Error
C. Cat1
D. Nothing. There’s no print statement.

B. Error

✔️ You cannot invoke methods defined in the Siamese class on an instance of the Cheshire class. Both are subclasses of Cat, but Cheshire is not a subclass of Siamese, so it doesn't inherit its methods.

What will the following code print (assuming we use the above definitions of Bird and Pet):

b2 = Bird('Sunny', 7)
print(b2.sounds)

A. 7
B. ["Mrrp"]
C. ["chirp"]
D. Error

C. ["chirp"]

✔️ The interpeter finds the value in the class variable for the class Bird.

For the Dog class defined in the earlier activecode window, what would happen when d1.feed() is run if the super().feed() line was deleted?

A. Error when invoked
B. The string "Arf! Thanks!" would not print out but d1 would still have its hunger reduced.
C. The string "Arf! Thanks!" would still print out but d1 would not have its hunger reduced.
D. Nothing would be different. It is the same as the current code.

C. The string "Arf! Thanks!" would still print out but d1 would not have its hunger reduced.

✔️ Since we are no longer calling the parent Pet class's method in the Dog subclass's method definition, the class definition will override the parent method.

Q-1: Consider the classes below. Which method(s) can be called on instances of type D?

class A(object):
    def x(self):
        print("X")

class B(A):
    def y(self):
        print("Y")

class C(A):
    def x(self):
        print("x")
    def z(self):
        print("Z")

class D(B):
    def z(self):
        print("z")

A. z
B. x and z
C. y and z
D. x, y, and z

D. x, y, and z

✔️ D is a subclass of B, which is a subclass of A. Therefore instances of class D may use methods from class D, B, or A. This includes the methods z, y, and x (respectively).

Q-1: Consider the classes below:

class A(object):
    def x(self):
        print("X")

class B(A):
    def y(self):
        print("Y")

class C(A):
    def x(self):
        print("x")
    def z(self):
        print("Z")

class D(B):
    def z(self):
        print("z")

What is the output of the following code?

my_instance = C()
my_instance.x()

A. x
B. X
C. self
D. None of the above

A. x

✔️ C is a subclass of A. The method 'x' in class A has been overridden in class C.

Recommended Books

Reading books is a great way to learn. Here are some of the books we recommend.