Decorators Python MCQs: MCQs On Decorators In Python

Decorators Python MCQs. Multiple Choice Questions related to Decorators In Python.

Decorators Python MCQs

 

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

 

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

 

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

 

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

MCQs 

 

A function with parameters cannot be decorated ??

a) True
b) False

 

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

 

The following python code can work with ____ parameters ??

def f(x):
def f1(*args, **kwargs):
print(“Myandroid”)
return x(*args, **kwargs)
return f1

a) 2
b) 1
c) any number of
d) 0

 

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

Decorators Python MCQs

 

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

 

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

 

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

 

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

Decorators Python MCQs

 

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

 

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()

 

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

 

Read More

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!