Python — Learn

Python Mcq Set 20

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
2 What does built-in function type do in context of classes?
✅ Correct Answer: 2
3 Which of the following is not a type of inheritance?
✅ Correct Answer: 1
4 What does built-in function help do in context of classes?
✅ Correct Answer: 3
5 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())
✅ Correct Answer: 2
6 What type of inheritance is illustrated in the following Python code? class A(): pass class B(): pass class C(A,B): pass
✅ Correct Answer: 2
7 What type of inheritance is illustrated in the following Python code? class A(): pass class B(A): pass class C(B): pass
✅ Correct Answer: 1
8 What does single-level inheritance mean?
✅ Correct Answer: 3
9 Which of the following statements isn’t true?
✅ Correct Answer: 3
10 What 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
13 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()
✅ Correct Answer: 4
14 What 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
15 What 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
16 Which of the following best describes polymorphism?
✅ Correct Answer: 4
17 What is the biggest reason for the use of polymorphism?
✅ Correct Answer: 3
18 What is the use of duck typing?
✅ Correct Answer: 3
19 What 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
20 What 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
21 What 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
22 hat 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
23 What 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
24 What 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
25 hat 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
26 What 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
27 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' obj2=B() print(obj2.two())
✅ Correct Answer: 4
28 Which of the following statements is true?
✅ Correct Answer: 1
29 Which of these is not a fundamental features of OOP?
✅ Correct Answer: 3
30 Which of the following is the most suitable definition for encapsulation?
✅ Correct Answer: 2
31 What 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
32 Methods 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
34 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() 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
36 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()
✅ Correct Answer: 3
37 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)
✅ Correct Answer: 1
38 Which of the following is false about protected class members?
✅ Correct Answer: 3
39 What 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
40 How many except statements can a try-except block have?
✅ Correct Answer: 4
41 When will the else part of try-except-else be executed?
✅ Correct Answer: 3
42 Is the following Python code valid? try: # Do something except: # Do something finally: # Do something
✅ Correct Answer: 4
43 Is the following Python code valid? try: # Do something except: # Do something else: # Do something
✅ Correct Answer: 4
44 Can one block of except statements handle multiple exception?
✅ Correct Answer: 1
45 When is the finally block executed?
✅ Correct Answer: 4
46 What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)
✅ Correct Answer: 2
47 What will be the output of the following Python code? def foo(): try: print(1) finally: print(2) foo()
✅ Correct Answer: 1
48 What 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
49 What happens when ‘1’ == 1 is executed?
✅ Correct Answer: 2
50 What will be the output of the following Python code? x=10 y=8 assert x>y, 'X too small'
✅ Correct Answer: 3
Back to All Quizzes