The Sports Zones
Home
MCQs
Python — Quiz
Python Mcq Set 20
50 questions · Test your knowledge
Home
/
MCQs
/
Python Mcq Set 20
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()
A
Error, the syntax of the invoking method is wrong
B
The program runs fine but nothing is printed
C
1 0
D
1 2
2
What does built-in function type do in context of classes?
A
Determines the object name of any value
B
Determines the class name of any value
C
Determines class description of any value
D
Determines the file name of any value
3
Which of the following is not a type of inheritance?
A
Double-level
B
Multi-level
C
Single-level
D
Multiple
4
What does built-in function help do in context of classes?
A
Determines the object name of any value
B
Determines the class identifiers of any value
C
Determines class description of any built-in type
D
Determines class description of any user-defined built-in type
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())
A
A A
B
A B
C
B B
D
An exception is thrown
6
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
7
What type of inheritance is illustrated in the following Python code? class A(): pass class B(A): pass class C(B): pass
A
Multi-level inheritance
B
Multiple inheritance
C
Hierarchical inheritance
D
Single-level inheritance
8
What does single-level inheritance mean?
A
A subclass derives from a class which in turn derives from another class
B
A single superclass inherits from multiple subclasses
C
A single subclass derives from a single superclass
D
Multiple base classes inherit a single derived class
9
Which of the following statements isn’t true?
A
A non-private method in a superclass can be overridde
B
A derived class is a subset of superclass
C
The value of a private variable in the superclass can be changed in the subclass
D
When invoking the constructor from a subclass, the constructor of superclass is automatically invoked
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()
A
3 0
B
3 1
C
0 1
D
An exception in thrown
11
What will be the output of the following Python code? >>> class A: pass >>> class B(A): pass >>> obj=B() >>> isinstance(obj,A)
A
True
B
False
C
Wrong syntax for isinstance() method
D
Invalid method for classes
12
Which of the following statements is true?
A
The __new__() method automatically invokes the __init__ method
B
The __init__ method is defined in the object class
C
The __eq(other) method is defined in the object class
D
The __repr__() method is defined in the object class
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()
A
1
B
0
C
Error, invalid syntax for object declaration
D
Error, private class member can’t be accessed in a subclass
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()
A
Error, invalid syntax for object declaration
B
Nothing is printed
C
5
D
Error, private class member can’t be accessed in a subclass
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()
A
5
B
Error, class member x has two values
C
3
D
Error, protected class member can’t be accessed in a subclass
16
Which of the following best describes polymorphism?
A
Ability of a class to derive members of another class as a part of its own definition
B
Means of bundling instance variables and methods in order to restrict access to certain class members
C
Focuses on variables and passing of variables to functions
D
Allows for objects of different types and behaviour to be treated as the same general type
17
What is the biggest reason for the use of polymorphism?
A
It allows the programmer to think at a more abstract level
B
There is less program code to write
C
The program will have a more elegant design and will be easier to maintain and update
D
Program code takes up less space
18
What is the use of duck typing?
A
More restriction on the type values that can be passed to a given method
B
No restriction on the type values that can be passed to a given method
C
Less restriction on the type values that can be passed to a given method
D
Makes the program code smaller
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()
A
1 1 1
B
1 2 3
C
‘1’ ‘1’ ‘1’
D
An exception is thrown
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()
A
11
B
2
C
1
D
An exception is thrown
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)
A
1 1 1
B
1 2 3
C
1’ ‘1’ ‘1’
D
An exception is thrown
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()
A
15
B
60
C
An exception is thrown
D
30
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()
A
Demo’s check Derived’s check
B
Demo’s check Demo’s check
C
Derived’s check Demo’s check
D
Syntax error
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()
A
15
B
30
C
An exception is thrown
D
60
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()
A
Demo’s check Derived’s check
B
Demo’s check Demo’s check
C
Derived’s check Demo’s check
D
Syntax error
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)
A
False
B
1
C
True
D
An exception is thrown
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())
A
A
B
An exception is thrown
C
A B
D
B
28
Which of the following statements is true?
A
A non-private method in a superclass can be overridden
B
A subclass method can be overridden by the superclass
C
A private method in a superclass can be overridden
D
Overriding isn’t possible in Python
29
Which of these is not a fundamental features of OOP?
A
Encapsulation
B
Inheritance
C
Instantiation
D
Polymorphism
30
Which of the following is the most suitable definition for encapsulation?
A
Ability of a class to derive members of another class as a part of its own definition
B
Means of bundling instance variables and methods in order to restrict access to certain class members
C
Focuses on variables and passing of variables to functions
D
Allows for implementation of elegant software that is well designed and easily modified
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)
A
The program has an error because there isn’t any function to return self.a
B
The program has an error because b is private and display(self) is returning a private member
C
The program has an error because b is private and hence can’t be printed
D
The program runs fine and 1 is printed
32
Methods of a class that provide access to private members of the class are called as ______ and ______
A
getters/setters
B
__repr__/__str__
C
user-defined functions/in-built functions
D
__init__/__del__
33
Which of these is a private data field? def Demo: def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1
A
__a
B
__b
C
__c__
D
__d__
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())
A
The program has an error because there isn’t any function to return self.a
B
The program has an error because b is private and display(self) is returning a private member
C
The program has an error because b is private and hence can’t be printed
D
The program runs fine and 1 is printed
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)
A
The program runs properly and prints 45
B
The program has an error because the value of members of a class can’t be changed from outside the class
C
The program runs properly and prints 1
D
The program has an error because the value of members outside a class can only be changed as self.a=45
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()
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
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)
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
he program runs fine but nothing is printed
38
Which of the following is false about protected class members?
A
They begin with one underscore
B
They can be accessed by subclasses
C
They can be accessed by name mangling method
D
They can be accessed within a class
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)
A
The program runs fine because name mangling has been properly implemented
B
Error because the member shape is a protected member
C
Error because the proper syntax for name mangling hasn’t been implemented
D
Error because the member shape is a private member
40
How many except statements can a try-except block have?
A
zero
B
one
C
more than one
D
more than zero
41
When will the else part of try-except-else be executed?
A
always
B
when an exception occurs
C
when no exception occurs
D
when an exception occurs in to except block
42
Is the following Python code valid? try: # Do something except: # Do something finally: # Do something
A
no, there is no such thing as finally
B
no, finally cannot be used with except
C
no, finally must come before except
D
yes
43
Is the following Python code valid? try: # Do something except: # Do something else: # Do something
A
no, there is no such thing as else
B
no, else cannot be used with except
C
no, else must come before except
D
yes
44
Can one block of except statements handle multiple exception?
A
yes, like except TypeError, SyntaxError [,…]
B
yes, like except [TypeError, SyntaxError]
C
no
D
none of the mentioned
45
When is the finally block executed?
A
when there is no exception
B
when there is an exception
C
only if some condition that has been specified is satisfied
D
always
46
What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)
A
1
B
2
C
3
D
error, there is more than one return statement in a single try-finally block
47
What will be the output of the following Python code? def foo(): try: print(1) finally: print(2) foo()
A
1 2
B
1
C
2
D
none of the mentioned
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")
A
someError has occurred
B
someError has not occurred
C
invalid code
D
none of the mentioned
49
What happens when ‘1’ == 1 is executed?
A
we get a True
B
we get a False
C
an TypeError occurs
D
a ValueError occurs
50
What will be the output of the following Python code? x=10 y=8 assert x>y, 'X too small'
A
Assertion Error
B
10 8
C
No output
D
108
Submit Quiz
Back to All Quizzes