
Q 1. 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()
Show Answer
Answer:-B. mk()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().Q 2. 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. &
Show 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.Q 3. 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
Show Answer
Answer:-A. Address OrdinaryExplanation
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.Q 4. 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
Show Answer
Answer:-A. TrueExplanation
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.Q 5. 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
Show Answer
Answer:-C. 0 Zero Division ErrorExplanation
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.Q 6. 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
Show Answer
Answer:-A. hello NOExplanation
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 NOQ 7. 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
Show Answer
Answer:-A. ***** %%%%% hello %%%%% *****Explanation
The code shown above uses multiple decorators. The output of this code is: ***** %%%%% hello %%%%% *****Q 8. 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
Show Answer
Answer:-C. any number ofExplanation
The code shown above shows a general decorator which can work with any number of arguments.Q 9. 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
Show Answer
Answer:-D. helloExplanation
In the code shown above, we have not passed any parameter to the function p. Hence the output of this code is: hello.Q 10. A function with parameters cannot be decorated.
A. True
B. False
Show Answer
Answer:-B. FalseExplanation
Any function, irrespective of whether or not it has parameters can be decorated. Hence the statement is false.Q 11. 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
Show Answer
Answer:-D. mkExplanation
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.Q 12. 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
Show Answer
Answer:-C. 100Explanation
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.Q 13. 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
Show Answer
Answer:-B. $100Explanation
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.Q 14 . 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
Show Answer
Answer:-B. 3Explanation
The code shown above returns the number of times a given function has been called. Hence the output of this code is: 3Q 15 . 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
Leave a Reply