Python — Quiz

Python Mcq Set 20

50 questions · Test your knowledge

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()
2 What does built-in function type do in context of classes?
3 Which of the following is not a type of inheritance?
4 What does built-in function help do in context of classes?
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())
6 What type of inheritance is illustrated in the following Python code? class A(): pass class B(): pass class C(A,B): pass
7 What type of inheritance is illustrated in the following Python code? class A(): pass class B(A): pass class C(B): pass
8 What does single-level inheritance mean?
9 Which of the following statements isn’t true?
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()
11 What will be the output of the following Python code? >>> class A: pass >>> class B(A): pass >>> obj=B() >>> isinstance(obj,A)
12 Which of the following statements is true?
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()
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()
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()
16 Which of the following best describes polymorphism?
17 What is the biggest reason for the use of polymorphism?
18 What is the use of duck typing?
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()
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()
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)
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()
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()
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()
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()
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)
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())
28 Which of the following statements is true?
29 Which of these is not a fundamental features of OOP?
30 Which of the following is the most suitable definition for encapsulation?
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)
32 Methods of a class that provide access to private members of the class are called as ______ and ______
33 Which of these is a private data field? def Demo: def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1
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())
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)
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()
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)
38 Which of the following is false about protected class members?
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)
40 How many except statements can a try-except block have?
41 When will the else part of try-except-else be executed?
42 Is the following Python code valid? try: # Do something except: # Do something finally: # Do something
43 Is the following Python code valid? try: # Do something except: # Do something else: # Do something
44 Can one block of except statements handle multiple exception?
45 When is the finally block executed?
46 What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)
47 What will be the output of the following Python code? def foo(): try: print(1) finally: print(2) foo()
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")
49 What happens when ‘1’ == 1 is executed?
50 What will be the output of the following Python code? x=10 y=8 assert x>y, 'X too small'
Back to All Quizzes