12. Inheritance and Polymorphism - 22 Questions


12. Inheritance and Polymorphism

12. Inheritance and Polymorphism

22 Questions
Created by Y. Daniel Liang - http://www.cs.armstrong.edu/liang/index.html
Free
Inheritance allows programmer to create a general class first then later extend it to more specialized class. It also allows programmer to write better code. Using inheritance you can inherit all access data fields and methods, plus you can add your own methods and fields, thus inheritance provide a way to organize code, rather than rewriting it from scratch.
  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()



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

    View Answer | Discuss in forum
  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()



    0 0
    0 1
    1 2
    0 2

    View Answer | Discuss in forum
  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)

     classChildClass(ParentClass):
         def __init__(self):
             super().__init__()
             self.__x = 2
             self.y = 20
             
     c = ChildClass()
     c.print()



    1 10
    1 20
    2 10
    2 20

    View Answer | Discuss in forum
  4. Suppose A is a subclass of B, to invoke the __init__ method in B from A, you write _________.



    B.__init__(self)
    super().__init__(self)
    B.__init__()
    none of these

    View Answer | Discuss in forum
  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()



    super().__init__(self)
    super().__init__()
    A.__init__()
    none of these

    View Answer | Discuss in forum
  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()



    2 0
    3 1
    4 0
    3 0

    View Answer | Discuss in forum
  7. Which of the following statements is true?



    A subclass is a subset of a superclass.
    When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.
    You can override a non-private method defined in a superclass.
    You can override a private method defined in a superclass.

    View Answer | Discuss in forum
  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()



    B's __new__() invoked and followed by A's __init__() invoked
    B's __new__() invoked followed by A's __new__() invoked
    B's __new__() invoked, followed by A's __init__() invoked, and followed by A's
    A's __init__() invoked and followed by A's __new__() invoked

    View Answer | Discuss in forum
  9. Which of the following statements is true?



    By default, the __new__() method invokes the __init__ method.
    The __new__() method is defined in the object class.
    The __init__() method is defined in the object class.
    All of the above

    View Answer | Discuss in forum
  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()



    1
    2
    10
    i is not accessible from b.

    View Answer | Discuss in forum
  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()

     



    C C C
    A B C
    A A A
    B B B

    View Answer | Discuss in forum
  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()



    C C C
    A B C
    A A A
    B B B

    View Answer | Discuss in forum
  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()



    True
    False
    2
    1

    View Answer | Discuss in forum
  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()



    Person's getInfo is called Person's getInfo is called
    Person's getInfo is called Student's getInfo is called
    Student's getInfo is called Person's getInfo is called
    Student's getInfo is called Student's getInfo is called

    View Answer | Discuss in forum
  15. 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()



    The __init__ method of class A is not called.
    The __init__ method of class A is called and it displays "i from A is 0".
    The __init__ method of class A is called and it displays "i from A is 40".
    The __init__ method of class A is called and it displays "i from A is 60".

    View Answer | Discuss in forum
  16. 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()



    The __init__ method of class A is not called.
    The __init__ method of class A is called and it displays "i from B is 0".
    The __init__ method of class A is called and it displays "i from B is 40".
    The __init__ method of class A is called and it displays "i from B is 60".

    View Answer | Discuss in forum
  17. To check whether an object o is an instance of class A, use _________.



    o.isinstance(A)
    A.isinstance(o)
    isinstance(o, A)
    isinstance(A, o)

    View Answer | Discuss in forum
  18. What relationship is appropriate for Company and Employee?



    association
    composition
    inheritance
    none of these

    View Answer | Discuss in forum
  19. What relationship is appropriate for Course and Faculty?



    association
    composition
    inheritance
    none of these

    View Answer | Discuss in forum
  20. What relationship is appropriate for Student and Person?



    association
    composition
    inheritance
    none of these

    View Answer | Discuss in forum
  21. What relationship is appropriate for House and Window?



    association
    composition
    inheritance
    none of these

    View Answer | Discuss in forum
  22. What relationship is appropriate for Account and Savings Account?



    association
    composition
    inheritance
    none of these

    View Answer | Discuss in forum