Q: 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()
Solution: The code shown above first prints the word “Decorated” and then “ordinary”. Hence the output of this code is: Decorated Ordinary.
Q: 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()
Solution: 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: 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.
Solution: 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: What will be the output of the following Python code? def ordi(): print("Ordinary") ordi ordi()
Solution: 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: 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)
Solution: 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: What will be the output of the following Python function? def f(p, q): return p%q f(0, 2) f(2, 0)
Solution: 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: 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)
Solution: 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
Q: 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")
Solution: The code shown above uses multiple decorators. The output of this code is: ***** %%%%% hello %%%%% *****
Q: The following python code can work with ____ parameters. def f(x): def f1(*args, **kwargs): print("Sanfoundry") return x(*args, **kwargs) return f1
Solution: The code shown above shows a general decorator which can work with any number of arguments.
Q: 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")
Solution: In the code shown above, we have not passed any parameter to the function p. Hence the output of this code is: hello.
You Have Score    | /10 |