WorksheetsPython - Class
Total questions: 11
Worksheet time: 6mins
Select correct python packages from following
numpy
opencv
math
matplotlib
decimal
What is called when a function is defined inside a class?
class
method
module
function
A variable that is defined inside a method and belongs only to the current instance of a class is known as?
Inheritance
Instance variable
Function overloading
Instantiation
What will be output for the folllowing code?
class test:
def __init__(self,a):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
Runs normally, doesn’t display anything
Displays 0, which is the automatic default value
Error as one argument is required while creating the object
Error as display function requires additional argument
Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?
A.__init__(self)
B.__init__(self)
A.__init__(B)
B.__init__(A)
What is the output of the following piece of 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()
Error because class B inherits A but variable x isn’t inherited
0 1
0 0
Error, the syntax of the invoking method is wrong
What does built-in function type do in context of classes?
Determines the object name of any value
Determines the class name of any value
Determines class description of any value
Determines the file name of any value
What type of inheritance is illustrated in the following piece of code?
class A():
pass
class B():
pass
class C(A,B):
pass
Multi-level inheritance
Multiple inheritance
Hierarchical inheritance
Single-level inheritance
What is the output of the code shown below?
g = (i for i in range(5))
type(g)
class <’loop’>
class <‘iteration’>
class <’range’>
class <’generator’>
What is the output of the following piece of 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()
5
Error, class member x has two values
3
Error, protected class member can’t be accessed in a subclass
What is the output of the following piece of 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()
15
60
An exception is thrown
30
