gs gs109 Python OOPs - Python Inheritance - Quiz No.1
gs gs109 Python Quiz
This quiz belongs to book/course code gs gs109 Python of gs organization. We have 134 quizzes available related to the book/course Python. This quiz has a total of 10 multiple choice questions (MCQs) to prepare and belongs to topic Python OOPs. NVAEducation wants its users to help them learn in an easy way. For that purpose, you are free to prepare online MCQs and quizzes.
NVAEducation also facilitates users to contribute in online competitions with other students to make a challenging situation to learn in a creative way. You can create one to one, and group competition on an topic of a book/course code. Also on NVAEducation you can get certifications by passing the online quiz test.
Question 1: Which of the following best describes inheritance?
Ability of a class to derive members of another class as a part of its own definition
Means of bundling instance variables and methods in order to restrict access to certain class members
Focuses on variables and passing of variables to functions
Allows for implementation of elegant software that is well designed and easily modified
Question 2: Which of the following statements is wrong about inheritance?
Protected members of a class can be inherited
The inheriting class is called a subclass
Private members of a class can be inherited and accessed
Inheritance is one of the features of OOP
Question 3: What will be the output of the following Python code?
class Demo: def __new__(self): self.__init__(self) print("Demo's __new__() invoked") def __init__(self): print("Demo's __init__() invoked") class Derived_Demo(Demo): def __new__(self): print("Derived_Demo's __new__() invoked") def __init__(self): print("Derived_Demo's __init__() invoked") def main(): obj1 = Derived_Demo() obj2 = Demo() main()
Derived_Demo’s __init__() invoked Derived_Demo's __new__() invoked Demo's __init__() invoked Demo's __new__() invoked
Derived_Demo's __new__() invoked Demo's __init__() invoked Demo's __new__() invoked
Derived_Demo's __new__() invoked Demo's __new__() invoked
Derived_Demo’s __init__() invoked Demo's __init__() invoked
Question 4: What will be the output of the following Python code?
class Test: def __init__(self): self.x = 0 class Derived_Test(Test): def __init__(self): self.y = 1 def main(): b = Derived_Test() print(b.x,b.y) main()
0 1
0 0
Error because class B inherits A but variable x isn’t inherited
Error because when object is created, argument must be passed like Derived_Test(1)
Question 5: What will be the output of the following Python code?
class A(): def disp(self): print("A disp()") class B(A): pass obj = B() obj.disp()
Invalid syntax for inheritance
Error because when object is created, argument must be passed
Nothing is printed
A disp()
Question 8: Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?
A.__init__(self)
B.__init__(self)
A.__init__(B)
B.__init__(A)
Question 9: What will be the output of the following Python code?
class Test: def __init__(self): self.x = 0 class Derived_Test(Test): def __init__(self): Test.__init__(self) self.y = 1 def main(): b = Derived_Test() print(b.x,b.y) main()
Error because class B inherits A but variable x isn’t inherited
0 0
0 1
Error, the syntax of the invoking method is wrong
Question 10: What will be the output of the following Python code?
class A: def __init__(self, x= 1): self.x = x class der(A): def __init__(self,y = 2): super().__init__() self.y = y def main(): obj = der() print(obj.x, obj.y) main()
Error, the syntax of the invoking method is wrong
The program runs fine but nothing is printed
1 0
1 2