Review each question and reveal answers to strengthen your understanding
1 What will be the output of the following Python code?
class A:
def __init__(self, x= 1):
self.x = x
class der(A):
def __init__(self,y = 2):
super().__init__()
self.y = y
def main():
obj = der()
print(obj.x, obj.y)
main()
✅ Correct Answer: 4
2What does built-in function type do in context of classes?
✅ Correct Answer: 2
3Which of the following is not a type of inheritance?
✅ Correct Answer: 1
4What does built-in function help do in context of classes?
✅ Correct Answer: 3
5What 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())
✅ Correct Answer: 2
6What type of inheritance is illustrated in the following Python code?
class A():
pass
class B():
pass
class C(A,B):
pass
✅ Correct Answer: 2
7What type of inheritance is illustrated in the following Python code?
class A():
pass
class B(A):
pass
class C(B):
pass
✅ Correct Answer: 1
8What does single-level inheritance mean?
✅ Correct Answer: 3
9Which of the following statements isn’t true?
✅ Correct Answer: 3
10What will be the output of the following Python code?
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()
✅ Correct Answer: 2
11 What will be the output of the following Python code?
>>> class A:
pass
>>> class B(A):
pass
>>> obj=B()
>>> isinstance(obj,A)
✅ Correct Answer: 1
12 Which of the following statements is true?
✅ Correct Answer: 3
13What 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()
✅ Correct Answer: 4
14What will be the output of the following Python code?
class A:
def __init__(self):
self._x = 5
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
✅ Correct Answer: 3
15What will be the output of the following Python code?
class A:
def __init__(self,x=3):
self._x = x
class B(A):
def __init__(self):
super().__init__(5)
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
✅ Correct Answer: 1
16Which of the following best describes polymorphism?
✅ Correct Answer: 4
17What is the biggest reason for the use of polymorphism?
✅ Correct Answer: 3
18What is the use of duck typing?
✅ Correct Answer: 3
19What will be the output of the following Python code?
class A:
def __str__(self):
return '1'
class B(A):
def __init__(self):
super().__init__()
class C(B):
def __init__(self):
super().__init__()
def main():
obj1 = B()
obj2 = A()
obj3 = C()
print(obj1, obj2,obj3)
main()
✅ Correct Answer: 1
20What will be the output of the following Python code?
class Demo:
def __init__(self):
self.x = 1
def change(self):
self.x = 10
class Demo_derived(Demo):
def change(self):
self.x=self.x+1
return self.x
def main():
obj = Demo_derived()
print(obj.change())
main()
✅ Correct Answer: 2
21What will be the output of the following Python code?
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)
✅ Correct Answer: 2
22hat will be the output of the following Python code?
class A:
def __init__(self):
self.multiply(15)
print(self.i)
def multiply(self, i):
self.i = 4 * i;
class B(A):
def __init__(self):
super().__init__()
def multiply(self, i):
self.i = 2 * i;
obj = B()
✅ Correct Answer: 4
23What will be the output of the following Python code?
class Demo:
def check(self):
return " Demo's check "
def display(self):
print(self.check())
class Demo_Derived(Demo):
def check(self):
return " Derived's check "
Demo().display()
Demo_Derived().display()
✅ Correct Answer: 1
24What will be the output of the following Python code?
class A:
def __init__(self):
self.multiply(15)
def multiply(self, i):
self.i = 4 * i;
class B(A):
def __init__(self):
super().__init__()
print(self.i)
def multiply(self, i):
self.i = 2 * i;
obj = B()
✅ Correct Answer: 2
25hat will be the output of the following Python code?
class Demo:
def __check(self):
return " Demo's check "
def display(self):
print(self.check())
class Demo_Derived(Demo):
def __check(self):
return " Derived's check "
Demo().display()
Demo_Derived().display()
✅ Correct Answer: 2
26What will be the output of the following Python code?
class A:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return 1
def __eq__(self, other):
return self.x * self.y == other.x * other.y
obj1 = A(5, 2)
obj2 = A(2, 5)
print(obj1 == obj2)
✅ Correct Answer: 3
27What 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'
obj2=B()
print(obj2.two())
✅ Correct Answer: 4
28Which of the following statements is true?
✅ Correct Answer: 1
29Which of these is not a fundamental features of OOP?
✅ Correct Answer: 3
30Which of the following is the most suitable definition for encapsulation?
✅ Correct Answer: 2
31What will be the output of the following Python code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def display(self):
return self.__b
obj = Demo()
print(obj.__b)
✅ Correct Answer: 3
32Methods of a class that provide access to private members of the class are called as ______ and ______
✅ Correct Answer: 1
33 Which of these is a private data field?
def Demo:
def __init__(self):
__a = 1
self.__b = 1
self.__c__ = 1
__d__= 1
✅ Correct Answer: 2
34What will be the output of the following Python code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
print(obj.get())
✅ Correct Answer: 4
35 What will be the output of the following Python code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
obj.a=45
print(obj.a)
✅ Correct Answer: 1
36What 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()
✅ Correct Answer: 3
37What 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)
✅ Correct Answer: 1
38Which of the following is false about protected class members?
✅ Correct Answer: 3
39What will be the output of the following Python code?
class objects:
def __init__(self):
self.colour = None
self._shape = "Circle"
def display(self, s):
self._shape = s
obj=objects()
print(obj._objects_shape)
✅ Correct Answer: 2
40How many except statements can a try-except block have?
✅ Correct Answer: 4
41When will the else part of try-except-else be executed?
✅ Correct Answer: 3
42Is the following Python code valid?
try:
# Do something
except:
# Do something
finally:
# Do something
✅ Correct Answer: 4
43Is the following Python code valid?
try:
# Do something
except:
# Do something
else:
# Do something
✅ Correct Answer: 4
44Can one block of except statements handle multiple exception?
✅ Correct Answer: 1
45When is the finally block executed?
✅ Correct Answer: 4
46What will be the output of the following Python code?
def foo():
try:
return 1
finally:
return 2
k = foo()
print(k)
✅ Correct Answer: 2
47What will be the output of the following Python code?
def foo():
try:
print(1)
finally:
print(2)
foo()
✅ Correct Answer: 1
48What will be the output of the following Python code?
try:
if '1' != 1:
raise "someError"
else:
print("someError has not occurred")
except "someError":
print ("someError has occurred")
✅ Correct Answer: 3
49What happens when ‘1’ == 1 is executed?
✅ Correct Answer: 2
50What will be the output of the following Python code?
x=10
y=8
assert x>y, 'X too small'