Chapter 12 Inheritance and Polymorphism
What is the output of the following code? class Parent:def __init__(self):self.__x = 1self.y = 10def print(self):print(self.__x * self.y)class Child(Parent):def __init__(self):super().__init__()self.__x = 2self.y = 20c = Child()c.print()
20
What is the output of the following code? class A: def __init__(self, i = 0): self.i = i def m1(self): self.i += 3 class B(A): def __init__(self, j = 0): super().__init__(4) self.j = j def m1(self): self.i += 2 b = B(1) b.m1() print(b.i) print(b.j)
6 1
You can override a non-private method defined in a superclass.
True
In OOP, terminology, a derived or child class is called_________, and a base or parent class is called _________.
a subclass, a superclass
which of the following is the correct syntax for defining a class named Table, which inherits from class named Furniture?
class Table(Furniture)
What is the output of the following code? class A: def __init__(self): self.setI(20) print("i in A is", self.i) def setI(self, i): self.i = 2 * i; class B(A): def __init__(self): super().__init__() print("i in B is", self.i) def setI(self, i): self.i = 3 * i; a = A() b = B()
i in A is 40 i in A is 60 i in B is 60
Line 10 has an error in the program below, in order to fix it you can _________. 1. class A:2. def __init__(self, i = 1): 3. self.i = i4.5. class B(A):6. def __init__(self, j = 2):7. self.j = j 8.9. b = B() 10. print(b.i)
invoke super().__init__() in class B's __init__ method
What is the output of the following code? class A:def __init__(self, i = 10):self.i = iclass B(A):def __init__(self, j = 2):super().__init__()self.j = jb = B()print(b.i ** b.j)
100
Of the two classes, Cherry, and Flavor, which would most likely be the subclass.
Cherry
In the following code what is the name of the base class? class CS1234(Course)
Course
A subclass may not override any method other than __init__ method.
False
When a class inherits another class, it is required to use all the data attributes and methods of the superclass.
False
When constructing an object from a subclass, its superclass's initializer is automatically invoked.
False
You can override a private method defined in a superclass.
False
You can override the initializer defined in a superclass.
False
________ allows a new class to inherit members of the class it extends.
Inheritance
Which of the following about Inheritance is not true?
Inheritance is a GUI library used for developing GUI programs.
What gives a program the ability to call the correct method depending on the type of object that is used to call it?
Polymorphism
In the following line of code, what is the name of the subclass? class Rose(Flower):
Rose
What will the following code display? class A: def __init__(self, i = 0): self.i = i def m1(self): self.i += 1 def __str__(self): return self.i x = A(8) print(x)
The program has an error
A subclass inherits accessible data fields and methods from its superclass, but it can also have other data fields and methods.
True
Analyze the code below: class A: def __new__(self): self.__init__(self) print("Anew", end = " ") def __init__(self): print("Ainit", end = " ") class B(A): def __new__(self): self.__init__(self) print("Bnew", end = " ") def __init__(self): print("Binit", end = " ") b = B() a = A() The above code displays the following output: Binit Bnew Ainit Anew
True
Each subclass has a method named __init__ that overrides the superclass's __init__ method.
True
If no inheritance is specified when a class is defined, it's superclass is object by default.
True
What will the following code display? class Person: def __getInfo(self): return "getP" def printPerson(self): print(self.__getInfo()) class Student(Person): def __getInfo(self): return "getS" Person().printPerson() Student().printPerson()
getP getP
What will the following code display? class Person: def getInfo(self): return "getP" def printPerson(self): print(self.getInfo()) class Student(Person): def getInfo(self): return "getS" Person().printPerson() Student().printPerson()
getP getS
In OOP terminology, __________ means a method may be invoked from different type of objects, and the capability of determining which method to invoke is know as __________.
polymorphism, dynamic binding
When there are several classes that have many common data fields/ attributes, it is better to write a(n) ___________ to hold all the general data.
superclass