wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python - Class

Total questions: 11

Worksheet time: 6mins

Name
Class
Date
1.

Select correct python packages from following

a)

numpy

b)

opencv

c)

math

d)

matplotlib

e)

decimal

2.

What is called when a function is defined inside a class?

a)

class

b)

method

c)

module

d)

function

3.

A variable that is defined inside a method and belongs only to the current instance of a class is known as?

a)

Inheritance

b)

Instance variable

c)

Function overloading

d)

Instantiation

4.

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()

a)

Runs normally, doesn’t display anything

b)

Displays 0, which is the automatic default value

c)

Error as one argument is required while creating the object

d)

Error as display function requires additional argument

5.

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)

A.__init__(self)

b)

B.__init__(self)

c)

A.__init__(B)

d)

B.__init__(A)

6.

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()

a)

Error because class B inherits A but variable x isn’t inherited

b)

0 1

c)

0 0

d)

Error, the syntax of the invoking method is wrong

7.

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

8.

What type of inheritance is illustrated in the following piece of 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

9.

What is the output of the code shown below?

g = (i for i in range(5))

type(g)

a)

class <’loop’>

b)

class <‘iteration’>

c)

class <’range’>

d)

class <’generator’>

10.

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()

a)

5

b)

Error, class member x has two values

c)

3

d)

Error, protected class member can’t be accessed in a subclass

11.

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()

a)

15

b)

60

c)

An exception is thrown

d)

30