PYTHON Decorators MCQs

  1. Which keyword is used to define a decorator in Python?
    a) @
    b) #
    c) $
    d) %
    View Answer

    Correct answer: A — @

  2. What will be printed?
    1 python
    2 def deco(func):
    3 def wrapper():
    4 return "Eduinq " + func()
    5 return wrapper
    6
    7 @deco
    8 def greet():
    9 return "Python"
    10
    11 print(greet())
    a) Eduinq Python
    b) Python Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq Python

  3. Which of the following is used to decorate a function?
    a) @decorator
    b) decorator()
    c) func.decorator
    d) None
    View Answer

    Correct answer: A — @decorator

  4. What is the output of print(callable(greet)) after decoration?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  5. Which of the following is used to preserve function metadata?
    a) functools.wraps
    b) decorator.wraps
    c) metadata.wraps
    d) None
    View Answer

    Correct answer: A — functools.wraps

  6. What will be printed?
    1 python
    2 import functools
    3 def deco(func):
    4 @functools.wraps(func)
    5 def wrapper():
    6 return func()
    7 return wrapper
    8
    9 @deco
    10 def show():
    11 """Eduinq Function"""
    12 return "Python"
    13
    14 print(show.__doc__)
    a) Eduinq Function
    b) None
    c) Error
    d) Python
    View Answer

    Correct answer: A — Eduinq Function

  7. Which of the following is used to decorate class methods?
    a) @classmethod
    b) @staticmethod
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  8. What is the output of print(Eduinq.show()) if show is decorated with @classmethod?
    a) Executes class method
    b) Error
    c) None
    d) show
    View Answer

    Correct answer: A — Executes class method

  9. Which of the following is used to decorate static methods?
    a) @staticmethod
    b) @classmethod
    c) @property
    d) None
    View Answer

    Correct answer: A — @staticmethod

  10. What will be printed?
    1 python
    2 class Eduinq:
    3 @staticmethod
    4 def add(a,b):
    5 return a+b
    6 print(Eduinq.add(2,3))
    a) 5
    b) Error
    c) None
    d) 23
    View Answer

    Correct answer: A — 5

  11. Which of the following is used to decorate property methods?
    a) @property
    b) @classmethod
    c) @staticmethod
    d) None
    View Answer

    Correct answer: A — @property

  12. What is the output of print(obj.x) if x is decorated with @property returning 10?
    a) 10
    b) Error
    c) None
    d) x
    View Answer

    Correct answer: A — 10

  13. Which of the following is used to define property setter?
    a) @x.setter
    b) @property.setter
    c) @classmethod
    d) None
    View Answer

    Correct answer: A — @x.setter

  14. What will be printed?
    1 python
    2 class Eduinq:
    3 def __init__(self):
    4 self._x = 0
    5 @property
    6 def x(self):
    7 return self._x
    8 @x.setter
    9 def x(self,value):
    10 self._x = value
    11
    12 obj = Eduinq()
    13 obj.x = 5
    14 print(obj.x)
    a) 5
    b) Error
    c) None
    d) x
    View Answer

    Correct answer: A — 5

  15. Which of the following is used to define property deleter?
    a) @x.deleter
    b) @property.deleter
    c) @classmethod
    d) None
    View Answer

    Correct answer: A — @x.deleter

  16. What is the output of print(hasattr(obj,"x")) after deleting property x?
    a) False
    b) True
    c) Error
    d) None
    View Answer

    Correct answer: A — False

  17. Which of the following is used to decorate generator functions?
    a) Any decorator with yield inside
    b) @generator
    c) @yield
    d) None
    View Answer

    Correct answer: A — Any decorator with yield inside

  18. What will be printed?
    1 python
    2 def deco(func):
    3 def wrapper():
    4 yield from func()
    5 return wrapper
    6
    7 @deco
    8 def gen():
    9 yield "Eduinq"
    10
    11 print(list(gen()))
    a) ['Eduinq']
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Eduinq']

  19. Which of the following is used to decorate asynchronous functions?
    a) async def with decorator
    b) @async
    c) @await
    d) None
    View Answer

    Correct answer: A — async def with decorator

  20. What is the output of print(callable(async_func)) if async_func is decorated?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  21. Which of the following is used to decorate functions with arguments?
    a) Wrapper function inside decorator
    b) @decorator(args)
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  22. What will be printed?
    1 python
    2 def deco(func):
    3 def wrapper(*args):
    4 return "Eduinq " + func(*args)
    5 return wrapper
    6
    7 @deco
    8 def greet(name):
    9 return name
    10
    11 print(greet("Python"))
    a) Eduinq Python
    b) Python Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq Python

  23. Which of the following is used to decorate functions with keyword arguments?
    a) def wrapper(**kwargs)
    b) def wrapper(*args)
    c) def wrapper(args)
    d) None
    View Answer

    Correct answer: A — def wrapper(**kwargs)

  24. What is the output of print(greet.name) if greet is decorated without functools.wraps?
    a) wrapper
    b) greet
    c) Error
    d) None
    View Answer

    Correct answer: A — wrapper

  25. Which of the following is used to decorate multiple functions with same decorator?
    a) Apply @decorator to each function
    b) Apply decorator once globally
    c) Both A and B
    d) None
    View Answer

    Correct answer: A — Apply @decorator to each function

  26. What will be printed?
    1 python
    2 def deco(func):
    3 def wrapper():
    4 return func().upper()
    5 return wrapper
    6
    7 @deco
    8 def show():
    9 return "eduinq"
    10
    11 print(show())
    a) EDUINQ
    b) eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — EDUINQ

  27. Which of the following is used to stack multiple decorators?
    a) @deco1 @deco2
    b) @deco1+deco2
    c) @deco1*deco2
    d) None
    View Answer

    Correct answer: A — @deco1 @deco2

  28. What is the output of below code?
    1 python
    2 def deco1(func):
    3 def wrapper():
    4 return "A " + func()
    5 return wrapper
    6
    7 def deco2(func):
    8 def wrapper():
    9 return "B " + func()
    10 return wrapper
    11
    12 @deco1
    13 @deco2
    14 def show():
    15 return "Eduinq"
    16
    17 print(show())
    a) A B Eduinq
    b) B A Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — A B Eduinq

  29. Which of the following is used to decorate classes?
    a) @decorator applied to class
    b) @classmethod
    c) @staticmethod
    d) None
    View Answer

    Correct answer: A — @decorator applied to class

  30. What will be printed?
    1 python
    2 def deco(cls):
    3 cls.msg = "Eduinq"
    4 return cls
    5
    6 @deco
    7 class Test:
    8 pass
    9
    10 print(Test.msg)
    a) Eduinq
    b) Error
    c) None
    d) msg
    View Answer

    Correct answer: A — Eduinq

  31. Which of the following is used to decorate functions for logging?
    a) Decorator adding print statements
    b) @log
    c) @debug
    d) None
    View Answer

    Correct answer: A — Decorator adding print statements

  32. What is the output of below code?
    1 python
    2 def log(func):
    3 def wrapper():
    4 print("Calling",func.__name__)
    5 return func()
    6 return wrapper
    7
    8 @log
    9 def show():
    10 return "Eduinq"
    11
    12 print(show())
    a) Calling show Eduinq
    b) Eduinq Calling show
    c) Error
    d) None
    View Answer

    Correct answer: A — Calling show Eduinq

  33. Which of the following is used to decorate functions for timing?
    a) Decorator using time module
    b) @time
    c) @clock
    d) None
    View Answer

    Correct answer: A — Decorator using time module

  34. What will be printed?
    1 python
    2 import time
    3 def timer(func):
    4 def wrapper():
    5 start = time.time()
    6 result = func()
    7 end = time.time()
    8 print("Time:",end-start)
    9 return result
    10 return wrapper
    11
    12 @timer
    13 def show():
    14 return "Eduinq"
    15
    16 print(show())
    a) Eduinq and Time value
    b) Eduinq only
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq and Time value

  35. Which of the following is used to decorate functions for authentication?
    a) Decorator checking user credentials
    b) @auth
    c) @login
    d) None
    View Answer

    Correct answer: A — Decorator checking user credentials

  36. What is the output of below code?
    1 python
    2 def auth(func):
    3 def wrapper(user):
    4 if user=="Eduinq":
    5 return func(user)
    6 else:
    7 return "Unauthorized"
    8 return wrapper
    9
    10 @auth
    11 def greet(user):
    12 return "Hello "+user
    13
    14 print(greet("Eduinq"))
    a) Hello Eduinq
    b) Unauthorized
    c) Error
    d) None
    View Answer

    Correct answer: A — Hello Eduinq

  37. Which of the following is used to decorate functions for caching?
    a) functools.lru_cache
    b) @cache
    c) @memoize
    d) None
    View Answer

    Correct answer: A — functools.lru_cache

  38. What will be printed?
    1 python
    2 import functools
    3 @functools.lru_cache(maxsize=None)
    4 def square(x):
    5 return x*x
    6
    7 print(square(4))
    a) 16
    b) Error
    c) None
    d) 4
    View Answer

    Correct answer: A — 16

  39. Which of the following is used to decorate functions for validation?
    a) Decorator checking arguments
    b) @validate
    c) @check
    d) None
    View Answer

    Correct answer: A — Decorator checking arguments

  40. What is the output of below code?
    1 python
    2 def validate(func):
    3 def wrapper(x):
    4 if x<0:
    5 return "Invalid"
    6 return func(x)
    7 return wrapper
    8
    9 @validate
    10 def square(x):
    11 return x*x
    12
    13 print(square(5))
    a) 25
    b) Invalid
    c) Error
    d) None
    View Answer

    Correct answer: A — 25

  41. Which of the following is used to decorate functions for synchronization?
    a) threading.Lock inside decorator
    b) @sync
    c) @threadsafe
    d) None
    View Answer

    Correct answer: A — threading.Lock inside decorator

  42. What will be printed?
    1 python
    2 import threading
    3 lock = threading.Lock()
    4 def sync(func):
    5 def wrapper():
    6 with lock:
    7 return func()
    8 return wrapper
    9
    10 @sync
    11 def show():
    12 return "Eduinq"
    13
    14 print(show())
    a) Eduinq
    b) Error
    c) None
    d) sync
    View Answer

    Correct answer: A — Eduinq

  43. Which of the following is used to decorate functions for retry logic?
    a) Decorator retrying on failure
    b) @retry
    c) @again
    d) None
    View Answer

    Correct answer: A — Decorator retrying on failure

  44. What is the output of below code?
    1 python
    2 def retry(func):
    3 def wrapper():
    4 for i in range(3):
    5 try:
    6 return func()
    7 except:
    8 continue
    9 return "Failed"
    10 return wrapper
    11
    12 @retry
    13 def show():
    14 return "Eduinq"
    15
    16 print(show())
    a) Eduinq
    b) Failed
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  45. Which of the following is used to decorate functions for monitoring?
    a) Decorator printing usage info
    b) @monitor
    c) @track
    d) None
    View Answer

    Correct answer: A — Decorator printing usage info

  46. What will be printed?
    1 python
    2 def monitor(func):
    3 def wrapper():
    4 print("Function called")
    5 return func()
    6 return wrapper
    7
    8 @monitor
    9 def show():
    10 return "Eduinq"
    11
    12 print(show())
    a) Function called Eduinq
    b) Eduinq Function called
    c) Error
    d) None
    View Answer

    Correct answer: A — Function called Eduinq

  47. Which of the following is used to decorate functions for debugging?
    a) Decorator printing arguments and results
    b) @debug
    c) @trace
    d) None
    View Answer

    Correct answer: A — Decorator printing arguments and results

  48. What is the output of below code?
    1 python
    2 def debug(func):
    3 def wrapper(*args):
    4 print("Args:",args)
    5 result = func(*args)
    6 print("Result:",result)
    7 return result
    8 return wrapper
    9
    10 @debug
    11 def add(a,b):
    12 return a+b
    13
    14 print(add(2,3))
    a) Args:(2,3) Result:5 5
    b) 5
    c) Error
    d) None
    View Answer

    Correct answer: A — Args:(2,3) Result:5 5

  49. Which of the following is used to decorate functions for memoization?
    a) functools.lru_cache
    b) @memoize
    c) @cache
    d) None
    View Answer

    Correct answer: A — functools.lru_cache

  50. What will be printed?
    1 python
    2 import functools
    3 @functools.lru_cache(maxsize=None)
    4 def square(x):
    5 return x*x
    6 print(square(5))
    a) 25
    b) Error
    c) None
    d) 5
    View Answer

    Correct answer: A — 25

Quick Links to Explore