Quiz: Intermediate Python
Test functions, modules, exceptions, decorators, and data structures with 10 intermediate questions.
Covers: Functions, Error Handling, Decorators, Data Structures
Q1. What does *args capture in a function?
- A) Keyword arguments
- B) Variable positional arguments as a tuple ✓
- C) Global variables
- D) Default values
Q2. What is the output?
def func(a, b=2, c=3):
return a + b + c
print(func(1))
print(func(1, 5))
- A)
6then9✓ - B) Error then
9 - C)
6then6 - D) Error on both
Q3. Which exception is raised by int("abc")?
- A)
TypeError - B)
ValueError✓ - C)
KeyError - D)
SyntaxError
Q4. What does finally do?
- A) Runs only on success
- B) Runs only on error
- C) Always runs regardless of exceptions ✓
- D) Re-raises the exception
Q5. What does @functools.lru_cache do?
- A) Deletes old functions
- B) Memoizes function results ✓
- C) Limits recursion depth
- D) Sorts function arguments
Q6. What is the time complexity of x in my_list for a list of n items?
- A) O(1)
- B) O(log n)
- C) O(n) ✓
- D) O(n²)
Sets and dicts offer O(1) membership testing.
Q7. What does import math as m do?
- A) Imports only the
mfunction - B) Imports math with alias
m✓ - C) Imports from a file named m
- D) Creates a new module
Q8. What is a generator?
- A) A class that generates random numbers
- B) A function that uses
yieldto produce values lazily ✓ - C) A type of import
- D) A database tool
Q9. What does raise ValueError("msg") from e do?
- A) Ignores the original error
- B) Chains the new exception to the original cause ✓
- C) Prints a warning
- D) Returns None
Q10. Which is the correct way to copy a nested list independently?
- A)
new = old.copy() - B)
new = list(old) - C)
new = copy.deepcopy(old)✓ - D)
new = old[:]
Shallow copies share inner objects. Use
copy.deepcopy()for full independence.
Scoring: 8+ = ready for Advanced Topics and Projects. Below 8 = review missed topics.