1What is the output of `print(type([]))` in Python?
Explanation
`[]` creates an empty list. `type([])` returns `<class 'list'>` in Python 3.
2Which of the following is an immutable data type in Python?
Explanation
Tuples are immutable — they cannot be changed after creation. Lists, dicts, and sets are all mutable.
3What does `len({'a': 1, 'b': 2, 'c': 3})` return?
Explanation
`len()` on a dict returns the number of key-value pairs, which is 3.
4What is the output of `[1, 2, 3][1:2]`?
Explanation
Slicing `[1:2]` starts at index 1 (inclusive) and ends at index 2 (exclusive), returning `[2]`.
5Which keyword is used to define a generator function in Python?
Explanation
`yield` turns a function into a generator. When called, it returns a generator object and produces values lazily.
6What will `bool([])` evaluate to?
Explanation
An empty list `[]` is falsy in Python. `bool([])` returns `False`. Non-empty containers are truthy.
7What does `*args` in a function definition collect?
Explanation
`*args` collects any number of extra positional arguments into a **tuple**. `**kwargs` collects keyword arguments into a dict.
8What is the result of `10 // 3` in Python?
Explanation
`//` is floor division — it divides and rounds down to the nearest integer. `10 // 3 = 3`.
9Which of these creates a shallow copy of a list `lst`?
Explanation
`lst.copy()`, `lst[:]`, and `list(lst)` all create shallow copies. `copy.deepcopy()` creates a deep copy.
10What is the output of `print('abc'[-1])`?
Explanation
Negative indexing counts from the end. Index `-1` refers to the last character, which is `'c'`.
11Which decorator is used to define a class method that receives the class as the first argument?
Explanation
`@classmethod` passes the class (`cls`) as the first argument instead of the instance. Commonly used for alternative constructors.
12What does the `__init__` method do in a Python class?
Explanation
`__init__` is the constructor — it is called automatically when a new instance is created and sets up initial state.
13What is the GIL in Python?
Explanation
The GIL (Global Interpreter Lock) is a mutex in CPython that allows only one thread to execute Python bytecode at a time, limiting true multi-core parallelism for CPU-bound tasks.
14Which module provides the `deepcopy` function?
Explanation
The `copy` module provides both `copy()` (shallow) and `deepcopy()` (deep) functions for copying Python objects.
15What is the output of `list(range(0, 10, 3))`?
Explanation
`range(0, 10, 3)` starts at 0, stops before 10, with step 3: 0, 3, 6, 9.
16What will `{1, 2, 3} & {2, 3, 4}` return?
Explanation
The `&` operator on sets performs intersection — elements common to both sets. `{1,2,3} & {2,3,4} = {2, 3}`.
17What does `functools.wraps` do when writing a decorator?
Explanation
`@functools.wraps(func)` copies the wrapped function's `__name__`, `__doc__`, `__module__`, etc. onto the wrapper so debugging tools see the original function's identity.
18Which of these is the correct way to open a file safely in Python?
Explanation
The `with` statement (context manager) automatically closes the file even if an exception occurs. The other approaches may leave the file open.
19What is the output of `print(2 ** 3 ** 2)`?
Explanation
The `**` operator is right-associative: `3 ** 2 = 9` first, then `2 ** 9 = 512`.
20What does `enumerate(['a', 'b', 'c'])` return?
Explanation
`enumerate()` returns an iterator of `(index, value)` tuples. To get a list, wrap it: `list(enumerate(['a','b','c']))` → `[(0,'a'),(1,'b'),(2,'c')]`.
21What is the difference between `is` and `==` in Python?
Explanation
`is` compares object identity (same memory address). `==` compares values. Always use `is` for `None`, `True`, `False`; use `==` for value comparisons.
22What happens when you call `next()` on an exhausted generator?
Explanation
Once a generator is exhausted (all values have been yielded), calling `next()` raises `StopIteration`. `for` loops handle this automatically.
23Which built-in function converts a string to an integer?
Explanation
`int('42')` returns the integer `42`. `int('3.14')` would raise a ValueError — use `int(float('3.14'))` for floats.
24What does the `@property` decorator do?
Explanation
`@property` allows you to define a method that is accessed like an attribute (without parentheses). You can add `@name.setter` and `@name.deleter` for full attribute control.
25What is the purpose of `__slots__` in a Python class?
Explanation
`__slots__` prevents `__dict__` creation per instance, reducing memory usage by 40-50% — especially beneficial when creating millions of objects.
26Which module would you use for asynchronous HTTP requests in Python?
Explanation
`aiohttp` is an async HTTP client/server framework that works with `asyncio`. The `requests` library is synchronous and blocks the event loop.
27What does `dict.get('key', 'default')` do when the key doesn't exist?
Explanation
`dict.get(key, default)` returns the value for `key` if it exists, or the `default` value if not — without raising a KeyError.
28What is a `lambda` function in Python?
Explanation
A `lambda` is an anonymous function defined with the `lambda` keyword. It can have any number of arguments but only one expression: `lambda x: x * 2`.
29What does `Counter('aabbbcc').most_common(2)` return?
Explanation
`Counter` counts occurrences. `most_common(2)` returns the 2 most frequent elements as `(element, count)` tuples sorted by count descending.
30Which of the following correctly uses `asyncio.gather()`?
Explanation
`asyncio.gather()` takes coroutine objects (not coroutine functions). Call the `async def` functions with `()` to get coroutines, then `await gather()` to run them concurrently.
Your First Python Program — Quick Check
1You saved print('Hello, World!') in hello.py. Your terminal is in the same folder, and Python 3 is available as python. Which command runs the file and displays Hello, World!?
Explanation
Run python hello.py from the folder containing the saved file. Python executes print('Hello, World!') and displays Hello, World! in the terminal. Depending on your installation, use python3 hello.py or py hello.py instead.