Test Series - python

Test Number 15/108

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()
A.  Decorated Decorated
B. Ordinary Ordinary
C.  Ordinary Decorated
D. Decorated Ordinary
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()
A. p()
B. mk()
C. mk1()
D. mk2()
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.
A. #
B. $
C. @
D. &
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()
A. Address Ordinary
B. Error Address
C. Ordinary Ordinary
D. Ordinary Address
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)
A. True
B. False
C. 
D. 
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)
A.  0 0
B. Zero Division Error Zero Division Error
C. 0 Zero Division Error
D. Zero Division Error 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)
A. hello NO
B. hello Zero Division Error
C. NO
D. hello
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")
A. ***** %%%%% hello %%%%% *****
B. Error
C. *****%%%%%hello%%%%%*****
D. 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
A. any number of
B. 2
C. 1
D. 0
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")
A. ***** hello
B.  ***** ***** hello
C. *****
D. 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