pythonintermediate
Python Intermediate Quiz
List comprehensions, decorators, generators, exception handling, and OOP in Python.
7 questions
By EZ4Code Team
1. What is the output of this list comprehension?
nums = [1, 2, 3, 4]
result = [x * 2 for x in nums if x % 2 == 0]
print(result)[2, 4, 6, 8]
[4, 8]
[2, 6]
[1, 3]
Explanation: The comprehension iterates over `nums`, keeps only even numbers (`x % 2 == 0` → 2 and 4), and doubles them. So `[2*2, 4*2] = [4, 8]`.
2. What does a decorator do in Python?
Adds styling to a class
Modifies the behavior of a function without changing its code
Declares a static variable
Creates a new data type
Explanation: A decorator is a function that takes another function and extends its behavior without modifying the original function's source code. It uses the `@decorator_name` syntax above the function definition.
3. What is the result of this generator expression?
gen = (x ** 2 for x in range(3))
print(list(gen))[0, 1, 2]
[0, 1, 4]
[1, 4, 9]
<generator object>
Explanation: `range(3)` yields 0, 1, 2. Squaring each gives 0, 1, 4. Wrapping with `list()` materializes the generator into a list. The generator itself prints as `<generator object>`, but `list(gen)` evaluates it.
4. Which exception is raised when dividing by zero?
result = 10 / 0DivideByZeroError
ZeroDivisionError
ArithmeticError
ValueError
Explanation: Python raises `ZeroDivisionError` when you divide by zero. `ArithmeticError` is the parent class, but the specific subclass is `ZeroDivisionError`. There is no `DivideByZeroError` in Python (that's a C# name).
5. What does `self` refer to in a Python class method?
class Dog:
def __init__(self, name):
self.name = nameThe class itself
The current instance of the class
The parent class
A static reference
Explanation: `self` refers to the current instance — it's the equivalent of `this` in Java/JavaScript. It's explicitly passed as the first parameter of instance methods. To reference the class itself, use `cls` (in classmethods) or the class name.
6. What is the output?
d = {"a": 1, "b": 2}
d.setdefault("c", 3)
d.setdefault("a", 99)
print(d){'a': 99, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2}
{'a': 99, 'b': 2}
Explanation: `setdefault(key, default)` only sets the value if the key is missing. `c` is missing, so it's set to 3. `a` already exists with value 1, so the call `setdefault('a', 99)` is a no-op — the value stays 1.
7. Which statement about `*args` and `**kwargs` is correct?
*args is a list, **kwargs is a list
*args is a tuple, **kwargs is a dict
*args is a dict, **kwargs is a tuple
Both are tuples
Explanation: Inside a function, `*args` collects positional arguments into a `tuple`, and `**kwargs` collects keyword arguments into a `dict`. The names `args` and `kwargs` are convention — only the `*` and `**` matter syntactically.
More python Quizzes
Python Basics Quiz
beginnerTest your understanding of Python fundamentals: syntax, types, control flow, and built-in functions.
Python Object-Oriented Programming
intermediateClasses, inheritance, polymorphism, magic methods and properties
Python Data Science
advancedpandas, numpy, matplotlib basics
Python Asynchronous Programming
advancedasyncio, coroutines, tasks
Python Decorators
intermediateDecorators, closures and higher-order functions