Table of Contents
This Python MCQ Questions and Answers are based on important and latest topics of Python programming.
Q1: What will be the output of the following Python code?
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()
Answer: d
Q2: What will be the output of the following Python code?
class Test:
def init(self):
self.x = 0
class Derived_Test(Test):
def init(self):
Test.init(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
a) Error because class B inherits A but variable x isn’t inherited
b) 0 0
c) 0 1
d) Error, the syntax of the invoking method is wrong
Answer: c
Q3: class is a _
a) template
b) blueprint
c) both a and b
d)None of the above
Answer: c
Q4: Which of the following is not a type of inheritance?
a) Double-level
b) Multi-level
c) Single-level
d) Multiple
Answer: a
Q5: 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
Answer: b
Q6: What will be the output of the following Python code?
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
Answer: b
Q7:What will be the output of the following Python 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
Answer: c
Q8:Which is not an object?
a) string
b) list
c) dictionary
d) None of the above
Answer: d
Q9: What will be the output of the following Python code?
class A:
def init(self):
self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()
a) 1
b) 0
c) None
d) Error
Answer: d
Q 10: What will be the output of the following Python code?
class A:
def test(self):
print(“test of A called”)
class B(A):
def test(self):
print(“test of B called”)
super().test()
class C(A):
def test(self):
print(“test of C called”)
super().test()
class D(B,C):
def test2(self):
print(“test of D called”)
obj=D()
obj.test()
a) test of B called
test of C called
test of A called
b) test of C called
test of B called
c) test of B called
test of C called
d) Error, all the three classes from which D derives has same method test()
Answer: a
Q11. 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
Answer: c
Q 12. 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
Answer: b
Q 13. del method is used to destroy instances of a class.
a) True
b) False
Answer: a
Q 14. What will be the output of the following Python code?
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(stud.doc)
a) Exception is thrown
b) main
c) Nothing is displayed
d) Base class for all students
Answer: d
Q 15. What does print(Test.name) display (assuming Test is the name of the class)?
a) ()
b) Exception is thrown
c) Test
d) main
Answer: c
Q 16:What will be the output of the following code?
print(False=={})
a)False
b)True
Answer: a
Q 17. What will be the output of the following Python code?
x=12
def f1(a,b=x):
print(a,b)
x=15
f1(4)
a) Error
b) 12 4
c) 4 12
d) 4 15
Answer: c
Q18: What will be the output of the following Python code?
def f1(a,b=[]):
b.append(a)
return b
print(f1(2,[3,4]))
a) [3,2,4]
b) [2,3,4]
c) Error
d) [3,4,2]
Answer: d
Q 19: What will be the output of the following Python code?
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
a) 1 2 3 4
b) 5 10 15 4
c) 10 20 30 40
d) 5 10 15 40
Answer: c
Q 20: What will be the output of the following Python code?
x = 5
def f1():
global x
x = 4
def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)
print(total)
a) Error
b) 7
c) 8
d) 15
Answer: b
Q 21: What will be the output of the following Python code?
x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)
a) 100
b) 90
c) 80
d) Error
Answer: a
Q 22) Read the following Python code carefully and point out the global variables?
y, z = 1, 2
def f():
global x
x = y+z
a) x
b) y and z
c) x, y and z
d) Neither x, nor y, nor z
Answer: c
Q 23) Which of these is a private data field?
class Demo:
def init(self,x,y,z):
self.a=x
self._b=y
self.__c=z
a) a
b) __c
c) _b
d) x
Answer: b
Q 24) What will be the output of the following Python code?
class fruits:
def init(self):
self.price = 100
self.__bags = 5
def display(self):
print(self.__bags)
obj=fruits()
obj.display()
a) The program has an error because display() is trying to print a private class member
b) The program runs fine but nothing is printed
c) The program runs fine and 5 is printed
d) The program has an error because display() can’t be accessed
Answer: c
Q 25) What will be the output of the following Python code?
class student:
def init(self):
self.marks = 97
self.__cgpa = 8.7
def display(self):
print(self.marks)
obj=student()
print(obj._student__cgpa)
a) The program runs fine and 8.7 is printed
b) Error because private class members can’t be accessed
c) Error because the proper syntax for name mangling hasn’t been implemented
d) The program runs fine but nothing is printed
Answer: a