wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Knowledge Knockout

Total questions: 16

Worksheet time: 11mins

Name
Class
Date
1.

What is the output?

def fun(n):

if n <= 1:

return n

return fun(n-1) + fun(n-2)

print(fun(6))

a)

6

b)

7

c)

8

d)

13

2.

Which Method Resolution Order (MRO) will Python follow?

class A: pass

class B(A): pass

class C(A): pass

class D(B, C): pass

print(D.mro())

a)

[D, B, A, C, object]

b)

[D, B, C, A, object]

c)

[D, C, B, A, object]

d)

Error due to diamond problem

3.

What will be the output?

class A:

def add(self, other):

return "A + Something"

class B:

def radd(self, other):

return "Something + B"

print(A() + B())

a)

A + Something

b)

Something + B

c)

Error

d)

None

4.

Which statement about private variables in Python is false?

a)

They are only accessible inside the class

b)

They are implemented by name mangling (_ClassName__var)

c)

They cannot be accessed from outside under any circumstance

d)

They start with double underscores __

5.

What is the time complexity of this function?

def foo(n):

i = 1

while i < n:

i *= 2

a)

O(n)

b)

O(log n)

c)

O(n log n)

d)

O(1)

6.

What will this code output?

class A:

x = 10

def init(self):

self.x = 20

a1 = A()

print(a1.x, A.x)

a)

20 20

b)

10 10

c)

20 10

d)

Error

7.

What will happen if base case is missing in recursion?

a)

Function executes once only

b)

Function gives wrong output but terminates

c)

Infinite recursion leading to RecursionError

d)

Function converts automatically to iteration

8.

What will the following print?

class A:

def show(self): return "A"

class B(A):

def show(self): return "B"

class C(A):

def show(self): return "C"

class D(B, C): pass

print(D().show())

a)

A

b)

B

c)

C

d)

Error

9.

Which is an example of runtime polymorphism in Python?

a)

Operator overloading

b)

Method overriding

c)

Default arguments

d)

Function overloading using multiple functions

10.

What is the time complexity of merging two sorted arrays of sizes n and m?

a)

O(log(n+m))

b)

O(n+m)

c)

O(n log m)

d)

O(max(n,m)

11.

What will this code print?

class Test:

def init(self):

self.__x = 5

def get(self):

return self.__x

t = Test()

print(t.get(), t._Test__x)

a)

5 5

b)

Error

c)

5 Error

d)

None 5

12.

What is the output?

def f(n):

if n == 0: return 1

return n * f(n//2)

print(f(10))

a)

10

b)

50

c)

100

d)

40

13.

What will happen here?

class A:

def init(self):

print("A init")

class B(A):

def init(self):

print("B init")

obj = B()

a)

Prints A init then B init

b)

Prints B init only

c)

Error

d)

Nothing

14.

What is the output?

class A:

def call(self): return "A"

class B(A):

def call(self): return "B"

def execute(obj):

print(obj.call())

execute(A())

execute(B())

a)

A A

b)

B B

c)

A B

d)

Error

15.

What is the time complexity of QuickSort in the worst case?

a)

O(n log n)

b)

O(n^2)

c)

O(n)

d)

O(log n)

16.

Draw an array as a train 🚂 with each box as a seat for numbers.