Object Oriented Programming Python: OOP MCQs Python

Object Oriented Programming Python. Multiple Choice Questions related to OOP concepts in Python Programming.

Object Oriented Programming Python

 

What does print(Test.__name__) display (assuming Test is the name of the class) ??

a) ()
b) Exception is thrown
c) Test
d) __main__

 

What will be the output of the following Python code ??

class Demo:
def __init__(self):
pass

def test(self):
print(__name__)

obj = Demo()
obj.test()

a) Exception is thrown
b) __main__
c) Demo
d) test

 

What will be the output of the ??

class stud:
‘Base class for all students’
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print(“Roll no : “, self.roll_no, “, Grade: “, self.grade)
print(student.__doc__)

 

a) Exception is thrown
b) __main__
c) Nothing is displayed
d) Base class for all students

 

What will be the output of ??

def __init__(self, price):
self.price = price
obj=fruits(50)

obj.quantity=10
obj.bags=2

print(obj.quantity+len(obj.__dict__))

 

a) 12
b) 52
c) 13
d) 60

Object Oriented Programming Python

 

__del__ method is used to destroy instances of a class ??

a) True
b) False

 

What is Instantiation in terms of OOP terminology ??

a) Deleting an instance of class
b) Modifying an instance of class
c) Copying an instance of class
d) Creating an instance of class

 

What is delattr(obj,name) used for ??

a) To print deleted attribute
b) To delete an attribute
c) To check if an attribute is deleted or not
d) To set an attribute

 

What will be the output ??

class test:
def __init__(self):
self.variable = ‘Old’
self.Change(self.variable)
def Change(self, var):
var = ‘New’
obj=test()
print(obj.variable)

a) Error because function change can’t be called in the __init__ function
b) ‘New’ is printed
c) ‘Old’ is printed
d) Nothing is printed

 

Object Oriented Programming Python

 

What will be the output of the following code ??

class stud:
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print(“Roll no : “, self.roll_no, “, Grade: “, self.grade)
stud1 = stud(34, ‘S’)
stud1.age=7
print(hasattr(stud1, ‘age’))

 

a) Error as age isn’t defined
b) True
c) False
d) 7

 

Is the following Python code correct ??

>>> class A:
def __init__(self,b):
self.b=b
def display(self):
print(self.b)
>>> obj=A(“Hello”)
>>> del obj

a) True
b) False

 

What is hasattr(obj,name) used for ??

a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute

 

Object Oriented Programming Python

 

What will be the output of code ??

class test:
def __init__(self,a):
self.a=a

def display(self):
print(self.a)
obj=test()
obj.display()

 

a) Runs normally, doesn’t display anything
b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the object
d) Error as display function requires additional argument

 

What will be the output of following ??

>>> class demo():
def __repr__(self):
return ‘__repr__ built-in function called’
def __str__(self):
return ‘__str__ built-in function called’
>>> s=demo()
>>> print(s)

 

a) __str__ called
b) __repr__ called
c) Error
d) Nothing is printed

 

What will be the output of the following Python code ??

class change:
def __init__(self, x, y, z):
self.a = x + y + z

x = change(1,2,3)
y = getattr(x, ‘a’)
setattr(x, ‘a’, y+1)
print(x.a)

a) 6
b) 7
c) Error
d) 0

More Posts

Leave a Comment

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

error: Content is protected !!