Python OOP: MCQs On OOP In Python

Python OOP. MCQs related to OOP concepts in Python language.

 

Python OOP

 

 

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

>>> 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) Error
b) Nothing is printed
c) __str__ called
d) __repr__ called

 

What is getattr() 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

 

Special methods need to be explicitly called during object creation ??

a) True
b) False

 

What is setattr() used for ??

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

 

Python OOP

 

What are the methods which begin and end with two underscore characters called ??

a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods

 

What will be the output ??

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

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

 

a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters

 

Is the following Python code valid ??

class B(object):
def first(self):
print(“First method called”)
def second():
print(“Second method called”)
ob = B()
B.first(ob)

a) It isn’t as the object declaration isn’t right
b) It isn’t as there isn’t any __init__ method for initializing class members
c) Yes, this method of calling is called unbounded method call
d) Yes, this method of calling is called bounded method call

 

What is used to create an object ??

a) class
b) constructor
c) User-defined functions
d) In-built functions

 

Python OOP

 

Which of the following Python code creates an empty class ??

a) class A:
return

b) class A:
pass

c) class A:

d) It is not possible to create an empty class

 

What represents an entity in the real world with its identity and behaviour ??

a) A method
b) An object
c) A class
d) An operator

 

What will be the output of the following code ??

def add(c,k):
c.test=c.test+1
k=k+1
class A:
def __init__(self):
self.test = 0
def main():
Count=A()
k=0

for i in range(0,25):
add(Count,k)
print(“Count.test=”, Count.test)
print(“k =”, k)
main()

a) Exception is thrown

b) Count.test=25
k=25

c) Count.test=25
k=0

d) Count.test=0
k=0

 

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

class A():
pass

class B():
pass

class C(A,B):
pass

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

 

Python OOP

 

Which of the following is not a class method ??

a) Non-static
b) Static
c) Bounded
d) Unbounded

 

What will be the output of the following ??

class A:
def one(self):
return self.two()

def two(self):
return ‘A’

class B(A):
def two(self):
return ‘B’
obj1=A()
obj2=B()
print(obj1.two(),obj2.two())

 

a) A A
b) A B
c) B B
d) An exception is thrown

 

The assignment of more than one function to a particular operator is ??

a) Operator over-assignment
b) Operator overriding
c) Operator overloading
d) Operator instance

 

Read More

Leave a Comment

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

error: Content is protected !!