OOPs In Python: OOP MCQs in Python

OOPs In Python. Questions And Answers Exercise on Concepts of OOPs In Python language.

OOPs In Python

 

Python is object oriented programming language ??

a) True
b) False

 

What will be the output of the following Python ??

class A():
def disp(self):
print(“A disp()”)
class B(A):
pass
obj = B()
obj.disp()

a) Invalid syntax for inheritance
b) Error because when object is created, argument must be passed
c) Nothing is printed
d) A disp()

 

Which of these is a private data field ??

def Demo:
def __init__(self):
__a = 1
self.__b = 1
self.__c__ = 1
__d__= 1

a) __a
b) __b
c) __c__
d) __d__

 

What will be the output of the ??

class A:
def __repr__(self):
return “1”
class B(A):
def __repr__(self):
return “2”
class C(B):
def __repr__(self):
return “3”
o1 = A()
o2 = B()
o3 = C()
print(obj1, obj2, obj3)

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

OOPs In Python

What will be the output of the following ??

>>> class A:
pass
>>> class B(A):
pass
>>> obj=B()
>>> isinstance(obj,A)

a) True
b) False
c) Wrong syntax for isinstance() method
d) Invalid method for classes

 

What will be the output of 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()

a) 0 1
b) 0 0
c) Error because class B inherits A but variable x isn’t inherited
d) Error because when object is created, argument must be passed like Derived_Test(1)

 

Methods of a class that provide access to private members of the class are called as ______ and ______ ??

a) getters/setters
b) __repr__/__str__
c) user-defined functions/in-built functions
d) __init__/__del__

 

Overriding means changing behaviour of methods of derived class methods in the base class ??

a) True
b) False

OOPs In Python

What will be the output of ??

class A:
def __init__(self,x):
self.x = x
def count(self,x):
self.x = self.x+1
class B(A):
def __init__(self, y=0):
A.__init__(self, 3)
self.y = y
def count(self):
self.y += 1
def main():
obj = B()
obj.count()
print(obj.x, obj.y)
main()

a) 3 0
b) 3 1
c) 0 1
d) An exception in thrown

 

What is the output ??

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()

 

a) Derived_Demo’s __init__() invoked
Derived_Demo’s __new__() invoked
Demo’s __init__() invoked
Demo’s __new__() invoked

b) Derived_Demo’s __new__() invoked
Demo’s __init__() invoked
Demo’s __new__() invoked

c) Derived_Demo’s __new__() invoked
Demo’s __new__() invoked

d) Derived_Demo’s __init__() invoked
Demo’s __init__() invoked

 

What will be the output of the code ??

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

def display(self):
return self.__b

obj = Demo()
print(obj.__b)

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 has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed

 

A class in which one or more methods are only implemented to raise an exception is called an abstract class ??

a) True
b) False

 

Which of the following statements isn’t true ??

a) A non-private method in a superclass can be overridden
b) A derived class is a subset of superclass
c) The value of a private variable in the superclass can be changed in the subclass
d) When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

More Posts

Leave a Comment

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

error: Content is protected !!