Python Functions and Scope 5 — Questions and Answers
Question 1: What is the difference between `return` and `yield` in Python?
- `return` is for generators; `yield` is for regular functions
- `return` exits the function and returns a value; `yield` pauses execution and produces a value (Correct answer)
- They are interchangeable
- `yield` only works inside a class
Correct answer: `return` exits the function and returns a value; `yield` pauses execution and produces a value
`return` terminates the function call, while `yield` suspends it and produces a value, resuming from the same point on the next iteration.
Question 2: Which of the following is a positional-only parameter syntax introduced in Python 3.8?
- def f(x, /, y): (Correct answer)
- def f(x, *, y):
- def f(@x, y):
- def f(x!, y):
Correct answer: def f(x, /, y):
The `/` separator in a parameter list marks all parameters before it as positional-only — they cannot be passed by keyword.
Question 3: What does `functools.partial` do?
- Returns a partial string of a function's source
- Creates a new callable with some arguments pre-filled (Correct answer)
- Makes a function run asynchronously
- Limits the number of times a function can be called
Correct answer: Creates a new callable with some arguments pre-filled
`functools.partial(func, *args, **kwargs)` returns a new callable with the given arguments pre-applied to `func`.
Question 4: What is the scope of a variable defined inside a `for` loop in Python?
- Block scope — it only exists inside the loop
- Function scope — it remains accessible after the loop (Correct answer)
- Global scope — it is visible everywhere
- It raises a ScopeError after the loop ends
Correct answer: Function scope — it remains accessible after the loop
Python has no block scope; loop variables remain in the enclosing function (or module) scope after the loop finishes.
Question 5: What does a decorator fundamentally do in Python?
- Compiles the function to bytecode immediately
- Wraps a function to extend or alter its behavior without modifying its source (Correct answer)
- Converts a method into a static function
- Enforces type annotations at runtime
Correct answer: Wraps a function to extend or alter its behavior without modifying its source
A decorator is a callable that takes a function, adds behavior, and returns a new callable — applied using the `@decorator` syntax.
Question 6: What is the output of: `def outer(): x = 10; def inner(): print(x); inner(); outer()`?
- NameError: x not defined
- 10 (Correct answer)
- None
- UnboundLocalError
Correct answer: 10
`inner` closes over `x` from `outer`'s enclosing scope and can read it, printing `10`.
Question 7: Which statement correctly describes recursive functions in Python?
- Python has no recursion limit
- Python enforces a default recursion limit (typically 1000) and raises RecursionError if exceeded (Correct answer)
- Recursive functions must use `yield`
- Recursion is only allowed in class methods
Correct answer: Python enforces a default recursion limit (typically 1000) and raises RecursionError if exceeded
Python sets a default recursion limit (usually 1000) accessible via `sys.getrecursionlimit()`; exceeding it raises `RecursionError`.
What is the difference between `return` and `yield` in Python?