Python Questions and Answers – Decorators

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Decorators”.

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
advertisement
advertisement

b)

    Ordinary
    Ordinary

c)

    Ordinary
    Decorated
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

d)

    Decorated
    Ordinary
View Answer
Answer: d
Explanation: 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?

advertisement
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()
View Answer

Answer: b
Explanation: 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().
advertisement

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) &
View Answer

Answer: c
Explanation: 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
View Answer
Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.