CMSC380 Introduction to Data Science
Quiz: Decorator and Closure
Name:
Put your answers in the box below
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
1. What is an inner function?
(A) A recursive function
(B) A lambda
(C) A built-in method
(D) A function defined inside another function
2. Which is true about closures?
(A) They retain access to outer scope
(B) They must return a class
(C) They require global
(D) They can’t return functions
3. What does the closure capture?
(A) Arguments only
(B) Free variables
(C) Return values
(D) Types
4. What will be printed?
def make_pow(n):
def power(x):
return x ** n
return power
square = make_pow(2)
print(square(4))
(A) 8
1
(B) 4
(C) 16
(D) Error
5. What does the ‘@’ symbol do in decorators?
(A) Defines a generator
(B) Creates a class
(C) Applies a decorator to a function
(D) Marks return type
6. What is returned by this?
def decorator(f):
def wrapper(): print(‘wrapped’)
return wrapper
def greet(): print(‘hello’)
greet = decorator(greet)
(A) greet
(B) wrapper function
(C) Error
(D) None
7. What is the primary use of decorators?
(A) Code reuse & augmentation
(B) Class inheritance
(C) Recursion
(D) Performance tuning
8. What is printed?
def log(f):
def wrapper():
print(‘logged’)
f()
return wrapper
@log
def task(): print(‘running’)
task()
(A) running
logged
(B) logged
(C) None
2
(D) logged
running
9. Which of the following creates a closure?
(A) Returning an inner function
(B) Calling a static method
(C) Defining init
(D) Using globals()
10. What is printed?
def outer():
x = 2
def inner(): return x + 5
return inner
f = outer()
print(f())
(A) 5
(B) 9
(C) 7
(D) Error
11. How do decorators work?
(A) They change variable types
(B) They delete memory
(C) They raise exceptions
(D) They wrap functions with other functions
12. What is printed?
def outer():
def inner(): return ‘outer’
return inner
print(outer()())
(A) inner
(B) outer
(C) Error
(D) None
13. What is the output?
def multiply(n):
def action(x): return x * n
3
return action
double = multiply(2)
print(double(50))
(A) 50
(B) 25
(C) 100
(D) Error
14. Which of the following best describes declarative programming?
(A) It describes what the program should accomplish.
(B) It describes how the program should execute step by step.
(C) It avoids recursion.
(D) It uses while loops for control flow.
15. What does a closure return in Python?
(A) A new list
(B) A function with an extended scope
(C) An object reference
(D) A lambda function
16. How is a closure related to a decorator?
(A) A decorator is a type of closure
(B) They are unrelated concepts
(C) Closures are only for recursion
(D) Closures cannot return functions
17. What is printed by this code?
def greet():
return "Hi"
functions = [greet, [Link], [Link]]
print(functions[0]())
(A) greet
(B) Hi
(C) None
(D) Error
18. What is returned by a closure function?
(A) A wrapped function with access to outer scope variables
(B) Only primitive types
(C) A string
4
(D) None
19. What does this function return?
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
c = counter()
print(c(), c())
(A) 0 1
(B) 1 1
(C) Error
(D) 1 2
20. In the following code, how many cache objects are created when calling fib(10)?
def memo_fib():
cache = {}
def fib(n):
if n in cache: return cache[n]
if n <= 1: result = n
else:
result = fib(n-1) + fib(n-2)
cache[n] = result
return result
return fib
fib = memo_fib()
print(fib(10)) # Output: 55
(A) A new cache object is created at every recursive call of fib(n).
(B) Eleven cache objects are created, one for each value from fib(0) to fib(10).
(C) Only one cache object is created inside memo fib() and shared by all recursive
calls.
(D) Ten cache objects are created, one for each recursive expansion from fib(10)
down to fib(1).