- Which keyword is used to define a decorator in Python?
View Answer
Correct answer: A — @
- 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()) View Answer
Correct answer: A — Eduinq Python
- Which of the following is used to decorate a function?
View Answer
Correct answer: A — @decorator
- What is the output of print(callable(greet)) after decoration?
View Answer
Correct answer: A — True
- Which of the following is used to preserve function metadata?
View Answer
Correct answer: A — functools.wraps
- 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__) View Answer
Correct answer: A — Eduinq Function
- Which of the following is used to decorate class methods?
View Answer
Correct answer: C — Both A and B
- What is the output of print(Eduinq.show()) if show is decorated with @classmethod?
View Answer
Correct answer: A — Executes class method
- Which of the following is used to decorate static methods?
View Answer
Correct answer: A — @staticmethod
- 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)) View Answer
Correct answer: A — 5
- Which of the following is used to decorate property methods?
View Answer
Correct answer: A — @property
- What is the output of print(obj.x) if x is decorated with @property returning 10?
View Answer
Correct answer: A — 10
- Which of the following is used to define property setter?
View Answer
Correct answer: A — @x.setter
- 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) View Answer
Correct answer: A — 5
- Which of the following is used to define property deleter?
View Answer
Correct answer: A — @x.deleter
- What is the output of print(hasattr(obj,"x")) after deleting property x?
View Answer
Correct answer: A — False
- Which of the following is used to decorate generator functions?
View Answer
Correct answer: A — Any decorator with yield inside
- 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())) View Answer
Correct answer: A — ['Eduinq']
- Which of the following is used to decorate asynchronous functions?
View Answer
Correct answer: A — async def with decorator
- What is the output of print(callable(async_func)) if async_func is decorated?
View Answer
Correct answer: A — True
- Which of the following is used to decorate functions with arguments?
View Answer
Correct answer: C — Both A and B
- 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")) View Answer
Correct answer: A — Eduinq Python
- Which of the following is used to decorate functions with keyword arguments?
View Answer
Correct answer: A — def wrapper(**kwargs)
- What is the output of print(greet.name) if greet is decorated without functools.wraps?
View Answer
Correct answer: A — wrapper
- Which of the following is used to decorate multiple functions with same decorator?
View Answer
Correct answer: A — Apply @decorator to each function
- 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()) View Answer
Correct answer: A — EDUINQ
- Which of the following is used to stack multiple decorators?
View Answer
Correct answer: A — @deco1 @deco2
- 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()) View Answer
Correct answer: A — A B Eduinq
- Which of the following is used to decorate classes?
View Answer
Correct answer: A — @decorator applied to class
- 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) View Answer
Correct answer: A — Eduinq
- Which of the following is used to decorate functions for logging?
View Answer
Correct answer: A — Decorator adding print statements
- 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()) View Answer
Correct answer: A — Calling show Eduinq
- Which of the following is used to decorate functions for timing?
View Answer
Correct answer: A — Decorator using time module
- 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()) View Answer
Correct answer: A — Eduinq and Time value
- Which of the following is used to decorate functions for authentication?
View Answer
Correct answer: A — Decorator checking user credentials
- 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")) View Answer
Correct answer: A — Hello Eduinq
- Which of the following is used to decorate functions for caching?
View Answer
Correct answer: A — functools.lru_cache
- 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)) View Answer
Correct answer: A — 16
- Which of the following is used to decorate functions for validation?
View Answer
Correct answer: A — Decorator checking arguments
- 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)) View Answer
Correct answer: A — 25
- Which of the following is used to decorate functions for synchronization?
View Answer
Correct answer: A — threading.Lock inside decorator
- 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()) View Answer
Correct answer: A — Eduinq
- Which of the following is used to decorate functions for retry logic?
View Answer
Correct answer: A — Decorator retrying on failure
- 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()) View Answer
Correct answer: A — Eduinq
- Which of the following is used to decorate functions for monitoring?
View Answer
Correct answer: A — Decorator printing usage info
- 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()) View Answer
Correct answer: A — Function called Eduinq
- Which of the following is used to decorate functions for debugging?
View Answer
Correct answer: A — Decorator printing arguments and results
- 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)) View Answer
Correct answer: A — Args:(2,3) Result:5 5
- Which of the following is used to decorate functions for memoization?
View Answer
Correct answer: A — functools.lru_cache
- 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)) View Answer
Correct answer: A — 25