Introduction to Programming Using Python, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 12 Inheritance and Polymorphism


Section 12.2 Superclasses and Subclasses
12.1  Analyze the following code:

class A:
    def __init__(self, i = 0):
        self.i = i

class B(A):
    def __init__(self, j = 0):
        self.j = j

def main():
    b = B()
    print(b.i)
    print(b.j)

main()
A. Class B inherits A, but the data field in i in A is not inherited.
B. Class B inherits A and automatically inherits all data fields in A.
C. When you create an object B, you have to pass an integer such as B(5).
D. The data field j cannot be accessed by object b.

12.2  What will be displayed by the following code?

class A:
    def __init__(self, i = 1):
        self.i = i

class B(A):
    def __init__(self, j = 2):
        super().__init__()
        self.j = j

def main():
    b = B()
    print(b.i, b.j)

main()
A. 0 0
B. 0 1
C. 1 2
D. 0 2
E. 2 1

12.3  What is the output of the following code?

 class ParentClass:
     def __init__(self):
         self.__x = 1
         self.y = 10
 
     def print(self):
         print(self.__x, self.y)

 class ChildClass(ParentClass):
     def __init__(self):
         super().__init__()
         self.__x = 2
         self.y = 20
         
 c = ChildClass()
 c.print()
A. 1 10
B. 1 20
C. 2 10
D. 2 20

12.4  Suppose A is a subclass of B, to invoke the __init__ method in B from A, you write _________.
A. super().__init__()
B. super().__init__(self)
C. B.__init__()
D. B.__init__(self)

12.5  What code can you put in the third line in class B to invoke B's superclass's constructor?

class A:
    def __init__(self, i = 1):
        self.i = i

class B(A):
    def __init__(self, j = 2):
        ___________________
        self.j = j

def main():
    b = B()
    print(b.i, b.j)

main()
A. super().__init__(self)
B. super().__init__()
C. A.__init__()
D. A.__init__(self)

Section 12.3 Overriding Methods
12.6  What will be displayed by the following code?

class A:
    def __init__(self, i = 0):
        self.i = i

    def m1(self):
        self.i += 1

class B(A):
    def __init__(self, j = 0):
        A.__init__(self, 3)
        self.j = j

    def m1(self):
        self.j += 1

def main():
    b = B()
    b.m1()
    print(b.i, b.j)

main()
A. 2 0
B. 3 1
C. 4 0
D. 3 0
E. 4 1

12.7  Which of the following statements is true?
A. A subclass is a subset of a superclass.
B. When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.
C. You can override a non-private method defined in a superclass.
D. You can override the initializer defined in a superclass.
E. You can override a private method defined in a superclass.

Section 12.4 The object Class
12.8  What will be displayed by the following code?

class A:
    def __new__(self):
        self.__init__(self)
        print("A's __new__() invoked")

    def __init__(self):
        print("A's __init__() invoked")


class B(A):
    def __new__(self):
        print("B's __new__() invoked")

    def __init__(self):
        print("B's __init__() invoked")


def main():
    b = B()
    a = A()

main()
A. B's __new__() invoked and followed by A's __init__() invoked
B. B's __new__() invoked followed by A's __new__() invoked
C. B's __new__() invoked, followed by A's __init__() invoked, and followed by A's __new__() invoked
D. A's __init__() invoked and followed by A's __new__() invoked

12.9  Which of the following statements is true?
A. By default, the __new__() method invokes the __init__ method.
B. The __new__() method is defined in the object class.
C. The __init__() method is defined in the object class.
D. The __str__() method is defined in the object class.
E. The __eq__(other) method is defined in the object class.

Section 12.5 Polymorphism and Dynamic Binding
12.10  What will be displayed by the following code?

class A:
    def __init__(self):
        self.i = 1

    def m(self):
        self.i = 10

class B(A):
    def m(self):
        self.i += 1
        return self.i


def main():
    b = B()
    print(b.m())

main()
A. 1
B. 2
C. 10
D. i is not accessible from b.

12.11  What will be displayed by the following code?

class A:
    def __str__(self):
        return "A"

class B(A):
    def __str__(self):
        return "B"

class C(B):
    def __str__(self):
        return "C"

def main():
    b = B()
    a = A()
    c = C()
    print(a, b, c)

main()

A. C C C
B. A B C
C. A A A
D. B B B

12.12  What will be displayed by the following code?

class A:
    def __str__(self):
        return "A"

class B(A):
    def __init__(self):
        super().__init__()

class C(B):
    def __init__(self):
        super().__init__()

def main():
    b = B()
    a = A()
    c = C()
    print(a, b, c)

main()
A. C C C
B. A B C
C. A A A
D. B B B

12.13  What will be displayed by the following code?

class A:
    def __init__(self, i = 2, j = 3):
        self.i = i
        self.j = j

    def __str__(self):
        return "A"

    def __eq__(self, other):
        return self.i * self.j == other.i * other.j

def main():
    x = A(1, 2)
    y = A(2, 1)
    print(x == y)

main()
A. True
B. False
C. 2
D. 1

12.14  What will be displayed by the following code?

class Person:
    def getInfo(self):
        return "Person's getInfo is called"
  
    def printPerson(self):
        print(self.getInfo(), end = ' ')

class Student(Person):
    def getInfo(self):
        return "Student's getInfo is called"

def main():
    Person().printPerson()
    Student().printPerson()

main()
A. Person's getInfo is called Person's getInfo is called
B. Person's getInfo is called Student's getInfo is called
C. Student's getInfo is called Person's getInfo is called
D. Student's getInfo is called Student's getInfo is called

12.15  What will be displayed by the following code?

class Person:
    def __getInfo(self):
        return "Person's getInfo is called"
  
    def printPerson(self):
        print(self.__getInfo(), end = ' ')

class Student(Person):
    def __getInfo(self):
        return "Student's getInfo is called"

def main():
    Person().printPerson()
    Student().printPerson()

main()
A. Person's getInfo is called Person's getInfo is called
B. Person's getInfo is called Student's getInfo is called
C. Student's getInfo is called Person's getInfo is called
D. Student's getInfo is called Student's getInfo is called

12.16  Analyze the following code:

class A:
    def __init__(self):
        self.setI(20)
        print("i from A is", self.i)

    def setI(self, i):
        self.i = 2 * i;

class B(A):
    def __init__(self):
        super().__init__()
        
    def setI(self, i):
        self.i = 3 * i;


b = B()
A. The __init__ method of class A is not called.
B. The __init__ method of class A is called and it displays "i from A is 0".
C. The __init__ method of class A is called and it displays "i from A is 40".
D. The __init__ method of class A is called and it displays "i from A is 60".

12.17  Analyze the following code:

class A:
    def __init__(self):
        self.setI(20)

    def setI(self, i):
        self.i = 2 * i;

class B(A):
    def __init__(self):
        super().__init__()
        print("i from B is", self.i)
        
    def setI(self, i):
        self.i = 3 * i;


b = B()
A. The __init__ method of class A is not called.
B. The __init__ method of class A is called and it displays "i from B is 0".
C. The __init__ method of class A is called and it displays "i from B is 40".
D. The __init__ method of class A is called and it displays "i from B is 60".

Section 12.6 The isinstance Function
12.18  To check whether an object o is an instance of class A, use _________.
A. o.isinstance(A)
B. A.isinstance(o)
C. isinstance(o, A)
D. isinstance(A, o)

Section 12.8 Class Relationships
12.19  What relationship is appropriate for Company and Employee?
A. association
B. composition
C. inheritance

12.20  What relationship is appropriate for Course and Faculty?
A. association
B. composition
C. inheritance

12.21  What relationship is appropriate for Student and Person?
A. association
B. composition
C. inheritance

12.22  What relationship is appropriate for House and Window?
A. association
B. composition
C. inheritance

12.23  What relationship is appropriate for Account and Savings Account?
A. association
B. composition
C. inheritance