Peer Instruction: Inheritance Multiple Choice Questions¶
- dog, cat
- Incorrect! A Dog is not a Cat.
- dog, animal
- Correct! A Dog is an Animal where Dog is the subclass and Animal is the superclass.
- animal, dog
- Incorrect! A Dog is an Animal where Dog is the subclass and Animal is the superclass and not the other way around.
- dog, tail
- Incorrect! A Dog has a Tail. It's not a "is-a" relationship.
- None of the above
- Incorrect! A Dog is an Animal where Dog is the subclass and Animal is the superclass.
Q-1: In the following pairs of words, the first is the subclass and the second is the superclass. Which of them is a correct example of inheritance?
- 5
- Incorrect! A Subclass only specifies what is different from its Superclass. Here B is the subclass and A is the superclass where only the "__init__" method is different them. Since "b = B(5)" has been called, the "__init__" method from B will be executed and not A. So, "x=5" will be multuplied by 2.
- 10
- Correct! A Subclass only specifies what is different from its Superclass. Here B is the subclass and A is the superclass where only the "__init__" method is different them. Since "b = B(5)" has been called, the "__init__" method from B will be executed and not A. So, "x=5" will be multuplied by 2.
- 510
- Incorrect! A Subclass only specifies what is different from its Superclass. Here B is the subclass and A is the superclass where only the "__init__" method is different them. Since "b = B(5)" has been called, the "__init__" method from B will be executed and not A. So, "x=5" will be multuplied by 2.
- This will cause an error
- Incorrect! This will not cause an error. A Subclass only specifies what is different from its Superclass. Here B is the subclass and A is the superclass where only the "__init__" method is different them. Since "b = B(5)" has been called, the "__init__" method from B will be executed and not A. So, "x=5" will be multuplied by 2.
- I don't know
- Incorrect! A Subclass only specifies what is different from its Superclass. Here B is the subclass and A is the superclass where only the "__init__" method is different them. Since "b = B(5)" has been called, the "__init__" method from B will be executed and not A. So, "x=5" will be multuplied by 2.
Q-2: What is the output of this code?
class A:
def __init__(self, x):
self.x = x
def __str__(self):
return str(self.x)
class B(A):
def __init__(self, x):
self.x = x * 2
b = B(5)
print(b)
- "Need to study...but...Internet!” will print twice
- Incorrect! There's a "study" attribute in Student but not in Person.
- "Need to study... but...Internet!” will print once
- Correct! There's a "study" attribute in Student but not in Person.
- Nothing will print
- Incorrect! There's a "study" attribute in Student but not in Person.
- This will cause an error
- Incorrect! There's a "study" attribute in Student but not in Person.
- I don't know
- Incorrect! There's a "study" attribute in Student but not in Person.
Q-3: What is the output of this code?
class Person:
count = 0
def __init__(self,first="J",last="Doe"):
self.first = first
self.last = last
Person.count = Person.count + 1
def __str__(self):
return self.first + " " + self.last
def work(self):
print("Gotta pay the bills")
def play(self):
print("TV time!")
def eat(self):
print("Woohoo, fast food!")
def sleep(self):
print("Zzzzzzzzzzzzzzzz")
class Student(Person):
def __init__(self,first,last,year):
Person.__init__(self,first,last)
self.year = year
def sleep(self):
print("So tired.... but... Internet!")
def study(self):
print("Need to study... but... Internet!")
def __str__(self):
return Person.__str__(self)+" '"+str(self.year%100)
p = Person(“George”, “Williker”)
s = Student(“Buddy”,”Bob”, "2014")
s.study()
p.study()
- 3, 1, 1
- Incorrect! Here "count" is a class variable which is shared by all objects of type A. It can be accessed by using A.count, A1.count, A2.count, and A3.count. "self.x" is unique for each object. So, "A1.x = 2 + 1 = 3" whereas "count" gets added by "1" thrice.
- 3, 1, 3
- Incorrect! Here "count" is a class variable which is shared by all objects of type A. It can be accessed by using A.count, A1.count, A2.count, and A3.count. "self.x" is unique for each object. So, "A1.x = 2 + 1 = 3" whereas "count" gets added by "1" thrice.
- 3, 3, 3
- Correct! Here "count" is a class variable which is shared by all objects of type A. It can be accessed by using A.count, A1.count, A2.count, and A3.count. "self.x" is unique for each object. So, "A1.x = 2 + 1 = 3" whereas "count" gets added by "1" thrice.
- 2, 1, 1
- Incorrect! Here "count" is a class variable which is shared by all objects of type A. It can be accessed by using A.count, A1.count, A2.count, and A3.count. "self.x" is unique for each object. So, "A1.x = 2 + 1 = 3" whereas "count" gets added by "1" thrice.
- I don't know
- Incorrect! Here "count" is a class variable which is shared by all objects of type A. It can be accessed by using A.count, A1.count, A2.count, and A3.count. "self.x" is unique for each object. So, "A1.x = 2 + 1 = 3" whereas "count" gets added by "1" thrice.
Q-4: What is the output of this code?
class A:
count = 0
def __init__(self, x):
self.x = x+1
A.count = A.count+1
A1 = A(2)
A2 = A(4)
A3 = A(5)
print(A1.x, A1.count, A.count)
You have attempted of activities on this page