Python Object Oriented: OOP Questions in Python

Python Object Oriented. This section contains Multiple Choice Questions on Python Object Oriented concepts.

Python Object Oriented

 

Which of the following statements is wrong about inheritance ??

a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP

 

What will be the answer ??

class Demo:
def __init__(self):
self.a = 1
self.__b = 1

def display(self):
return self.__b
obj = Demo()
print(obj.a)

a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a private member
c) The program runs fine and 1 is printed
d) The program has an error as you can’t name a class member using __b

 

What will be the output of the following ??

class Demo:
def __init__(self):
self.x = 1
def change(self):
self.x = 10
class Demo_derived(Demo):
def change(self):
self.x=self.x+1
return self.x
def main():
obj = Demo_derived()
print(obj.change())
main()

a) 11
b) 2
c) 1
d) An exception is thrown

 

What will be the output of the code ??

class A:
def __init__(self):
self.__i = 1
self.j = 5

def display(self):
print(self.__i, self.j)
class B(A):
def __init__(self):
super().__init__()
self.__i = 2
self.j = 7
c = B()
c.display()

a) 2 7
b) 1 5
c) 1 7
d) 2 5

Python Object Oriented

 

Which of the following best describes inheritance ??

a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and easily modified

 

Which of the following is the most suitable definition for encapsulation ??

a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and easily modified

 

What will be the output of the following Python ??

class A:
def __str__(self):
return ‘1’
class B(A):
def __init__(self):
super().__init__()
class C(B):
def __init__(self):
super().__init__()
def main():
obj1 = B()
obj2 = A()
obj3 = C()
print(obj1, obj2,obj3)
main()

a) 1 1 1
b) 1 2 3
c) ‘1’ ‘1’ ‘1’
d) An exception is thrown

 

What does single-level inheritance mean ??

a) A subclass derives from a class which in turn derives from another class
b) A single superclass inherits from multiple subclasses
c) A single subclass derives from a single superclass
d) Multiple base classes inherit a single derived class

 

Python Object Oriented

 

Which of these is not a fundamental features of OOP ??

a) Encapsulation
b) Inheritance
c) Instantiation
d) Polymorphism

 

What is the use of duck typing ??

a) More restriction on the type values that can be passed to a given method
b) No restriction on the type values that can be passed to a given method
c) Less restriction on the type values that can be passed to a given method
d) Makes the program code smaller

 

What type of inheritance is illustrated in the following Python code ??

class A():
pass
class B(A):
pass
class C(B):
pass

a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance

 

Which of the following best describes polymorphism ??

a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for objects of different types and behaviour to be treated as the same general type

 

What is the biggest reason for the use of polymorphism ??

a) It allows the programmer to think at a more abstract level
b) There is less program code to write
c) The program will have a more elegant design and will be easier to maintain and update
d) Program code takes up less space

More Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!