Python MCQs on Decorators

Python MCQs on Decorators

In this section, we are going to introduce you to the most frequently asked Python MCQs on Decorators. These Python MCQs on Decorators will be going to help you in Interviews and Exams.

 

1. What will be the output of the following Python code?

def mk(x):

def mk1():

print(“Decorated”)

x()

return mk1

def mk2():

print(“Ordinary”)

p = mk(mk2)

p()

a)

Decorated
Decorated

b)

Ordinary
Ordinary

c)

Ordinary

Decorated

d)

Decorated
Ordinary

Check Answer
Answer: d
The code shown above first prints the word “Decorated” and then “ordinary”. Hence the output of this code is:
Decorated
Ordinary.

 

 

2. In the following Python code, which function is the decorator?

def mk(x):

def mk1():

print(“Decorated”)

x()

return mk1

def mk2():

print(“Ordinary”)

p = mk(mk2)

p()

a) p()

b) mk()

c) mk1()

d) mk2()

Check Answer

Answer: b
In the code shown above, the function mk() is the decorator. The function which is getting decorated is mk2(). The return function is given the name p().

 

 

3. The ______ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.

a) #

b) $

c) @

d) &

Check Answer
Answer: c
The @ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.

 

 

4. What will be the output of the following Python code?

def ordi():

print(“Ordinary”)

ordi

ordi()

a)

Address
Ordinary

b)

Error
Address

c)

Ordinary
Ordinary

d)

Ordinary
Address

Check Answer

Answer: a
The code shown above returns the address on the function ordi first, after which the word “Ordinary” is printed. Hence the output of this code is:
Address
Ordinary.

 

 

5. The two snippets of the following Python codes are equivalent.

CODE 1

@f

def f1():

print(“Hello”)

CODE 2

def f1():

print(“Hello”)

f1 = f(f1)

a) True

b) False

Check Answer
Answer: a
The @ symbol can be used as an alternate way to specify a function that needs to be decorated. The output of the codes shown above is the same. Hence they are equivalent. Therefore this statement is true.

 

 

6. What will be the output of the following Python function?

def f(p, q):

return p%q

f(0, 2)

f(2, 0)

a)

0
0

b)

Zero Division Error
Zero Division Error

c)

0
Zero Division Error

d)

Zero Division Error
0

Check Answer

Answer: c
The output of f(0, 2) is 0, since o%2 is equal to 0. The output of the f(2, 0) is a Zero Division Error. We can make use of decorators in order to avoid this error.

 

 

7. What will be the output of the following Python code?

def f(x):

def f1(a, b):

print(“hello”)

if b==0:

print(“NO”)

return

return f(a, b)

return f1

@f

def f(a, b):

return a%b

f(4,0)

a)

hello
NO

b)

hello
Zero Division Error

c) NO

d) hello

Check Answer

Answer: a
In the code shown above, we have used a decorator in order to avoid the Zero Division Error. Hence the output of this code is:
hello
NO

 

 

8. What will be the output of the following Python code?

def f(x):

def f1(*args, **kwargs):

print(“*”* 5)

x(*args, **kwargs)

print(“*”* 5)

return f1

def a(x):

def f1(*args, **kwargs):

print(“%”* 5)

x(*args, **kwargs)

print(“%”* 5)

return f1

@f

@a

def p(m):

print(m)

p(“hello”)

a)

*****

%%%%%
hello
%%%%%
*****
b) Error

c) *****%%%%%hello%%%%%*****

d) hello

Check Answer

Answer: a
The code shown above uses multiple decorators. The output of this code is:
*****
%%%%%
hello
%%%%%
*****

 

 

9. The following python code can work with ____ parameters.

def f(x):

def f1(*args, **kwargs):

print(“Sanfoundry”)

return x(*args, **kwargs)

return f1

a) 2

b) 1

c) any number of

d) 0

Check Answer

Answer: c
The code shown above shows a general decorator which can work with any number of arguments.

 

 

10. What will be the output of the following Python code?

def f(x):

def f1(*args, **kwargs):

print(“*”, 5)

x(*args, **kwargs)

print(“*”, 5)

return f1

@f

def p(m):

p(m)

print(“hello”)

a)

*****
hello

b)

*****
*****
hello

c) *****

d) hello

Check Answer

Answer: d
In the code shown above, we have not passed any parameter to the function p. Hence the output of this code is: hello.

 

11. A function with parameters cannot be decorated.

a) True

b) False

Check Answer

Answer: b
Any function, irrespective of whether or not it has parameters can be decorated. Hence the statement is false.

 

 

12. Identify the decorator in the snippet of code shown below.

def sf():

pass

sf = mk(sf)

@f

def sf():

return

a) @f

b) f

c) sf()

d) mk

Check Answer

Answer: d
In the code shown above, @sf is not a decorator but only a decorator line. The ‘@’ symbol represents the application of a decorator. The decorator here is the function mk.

 

 

13. What will be the output of the following Python code?

class A:
@staticmethod
def a(x):
print(x)
A.a(100)

a) Error

b) Warning

c) 100

d) No output

Check Answer

Answer: c
The code shown above demonstrates rebinding using a static method. This can be done with or without a decorator. The output of this code will be 100.

 

 

14. What will be the output of the following Python code?

def d(f):
def n(*args):
return ‘$’ + str(f(*args))
return n
@d
def p(a, t):
return a + a*t
print(p(100,0))

a) 100

b) $100

c) $0

d) 0

Check Answer

Answer: b
In the code shown above, the decorator helps us to prefix the dollar sign along with the value. Since the second argument is zero, the output of the code is: $100.

 

 

15. What will be the output of the following Python code?

def c(f):

def inner(*args, **kargs):

inner.co += 1

return f(*args, **kargs)

inner.co = 0

return inner

@c

def fnc():

pass

if __name__ == ‘__main__’:

fnc()

fnc()

fnc()

print(fnc.co)

a) 4

b) 3

c) 0

d) 1

Check Answer
Answer: b
The code shown above returns the number of times a given function has been called. Hence the output of this code is: 3

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

 

 

Leave a Comment