WorksheetsKnowledge Knockout
Total questions: 16
Worksheet time: 11mins
What is the output?
def fun(n):
if n <= 1:
return n
return fun(n-1) + fun(n-2)
print(fun(6))
6
7
8
13
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())
[D, B, A, C, object]
[D, B, C, A, object]
[D, C, B, A, object]
Error due to diamond problem
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 + Something
Something + B
Error
None
Which statement about private variables in Python is false?
They are only accessible inside the class
They are implemented by name mangling (_ClassName__var)
They cannot be accessed from outside under any circumstance
They start with double underscores __
What is the time complexity of this function?
def foo(n):
i = 1
while i < n:
i *= 2
O(n)
O(log n)
O(n log n)
O(1)
What will this code output?
class A:
x = 10
def init(self):
self.x = 20
a1 = A()
print(a1.x, A.x)
20 20
10 10
20 10
Error
What will happen if base case is missing in recursion?
Function executes once only
Function gives wrong output but terminates
Infinite recursion leading to RecursionError
Function converts automatically to iteration
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
B
C
Error
Which is an example of runtime polymorphism in Python?
Operator overloading
Method overriding
Default arguments
Function overloading using multiple functions
What is the time complexity of merging two sorted arrays of sizes n and m?
O(log(n+m))
O(n+m)
O(n log m)
O(max(n,m)
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)
5 5
Error
5 Error
None 5
What is the output?
def f(n):
if n == 0: return 1
return n * f(n//2)
print(f(10))
10
50
100
40
What will happen here?
class A:
def init(self):
print("A init")
class B(A):
def init(self):
print("B init")
obj = B()
Prints A init then B init
Prints B init only
Error
Nothing
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
B B
A B
Error
What is the time complexity of QuickSort in the worst case?
O(n log n)
O(n^2)
O(n)
O(log n)
Draw an array as a train 🚂 with each box as a seat for numbers.

