Python OOPs MCQs
This section focuses on the "oops" in Python. These Multiple Choice Questions (mcq) should be practiced to improve the Python skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following is correct with respect to OOP concept in Python?
A. Objects are real world entities while classes are not real.
B. Classes are real world entities while objects are not real.
C. Both objects and classes are real world entities.
D. Both object and classes are not real.
View Answer
Ans : A
Explanation: In OOP, classes are basically the blueprint of the objects. They doesnot have physical existence.
2. How many objects and reference variables are there for the given Python code?
class A:
print("Inside class")
A()
A()
obj=A()
A. 2 and 1
B. 3 and 3
C. 3 and 1
D. 3 and 2
View Answer
Ans : C
Explanation: obj is the reference variable here and an object will be created each time A() is called.So there will be 3 objects created.
3. Which of the following is False with respect Python code?
class Student:
def __init__(self,id,age):
self.id=id
self.age=age
std=Student(1,20)
A. "std" is the reference variable for object Student(1,20)
B. id and age are called the parameters.
C. Every class must have a constructor.
D. None of the above
View Answer
Ans : C
Explanation: It is not mandatory for a class to have a constructor.
4. What will be the output of below Python code?
class Student:
def __init__(self,name,id):
self.name=name
self.id=id
print(self.id)
std=Student("Simon",1)
std.id=2
print(std.id)
A. 1
1
B. 1
2
C. 2
1
D. 2
2
View Answer
Ans : B
Explanation: When object with id =1 is created for Student, constructor is invoked and it prints 1. Later, id has been changed to 2, so next print statement prints 2.
5. What will be the output of below Python code?
class A():
def __init__(self,count=100):
self.count=count
obj1=A()
obj2=A(102)
print(obj1.count)
print(obj2.count)
A. 100
100
B. 100
102
C. 102
102
D. Error
View Answer
Ans : B
Explanation: By default,the value of "count" is 100, so obj1.count=100. For second object, value of "count" is 102, so obj2.count=102.
6. Which of the following is correct?
class A:
def __init__(self,name):
self.name=name
a1=A("john")
a2=A("john")
A. id(a1) and id(a2) will have same value.
B. id(a1) and id(a2) will have different values.
C. Two objects with same value of attribute cannot be created.
D. None of the above
View Answer
Ans : B
Explanation: Although both a1 and a2 have same value of attributes,but these two point to two different object. Hence, their id will be different.
7. Which of the following is correct?
class A:
def __init__(self):
self.count=5
self.count=count+1
a=A()
print(a.count)
A. 5
B. 6
C. 0
D. Error
View Answer
Ans : D
Explanation: It will throw an error as inside constructor, "count" is not defined.
8. Which of the following is correct?
class Book:
def __init__(self,author):
self.author=author
book1=Book("V.M.Shah")
book2=book1
A. Both book1 and book2 will have reference to two different objects of class Book.
B. id(book1) and id(book2) will have same value.
C. It will throw error as multiple references to same object is not possible.
D. None of the above
View Answer
Ans : B
Explanation: book1 and book2 will reference to the same object. Hence, id(book1) and id(book2) will have same value.
9. In python, what is method inside class?
A. attribute
B. object
C. argument
D. function
View Answer
Ans : D
Explanation: In OOP of Python, function is known by "method".
10. What will be the output of below Python code?
class A:
def __init__(self,num):
num=3
self.num=num
def change(self):
self.num=7
a=A(5)
print(a.num)
a.change()
print(a.num)
A. 5
7
B. 5
5
C. 3
3
D. 3
7
View Answer
Ans : D
Explanation: Inside constructor, self.num=3. First print statement prints 3. As, method change() is invoked, self.num=7 and hence second print statement prints 7.
Discussion