Python Python 2 — Questions and Answers
Question 1: What does the `*args` syntax in a function definition allow?
- Accept any number of keyword arguments
- Accept any number of positional arguments (Correct answer)
- Unpack a dictionary into arguments
- Define default argument values
Correct answer: Accept any number of positional arguments
`*args` collects extra positional arguments into a tuple inside the function.
Question 2: Which method removes and returns the last element of a Python list?
- remove()
- del()
- pop() (Correct answer)
- discard()
Correct answer: pop()
`list.pop()` removes and returns the last element by default, or an element at a given index.
Question 3: What is the output of `bool([])` in Python?
- True
- False (Correct answer)
- None
- Error
Correct answer: False
An empty list is falsy in Python, so `bool([])` returns `False`.
Question 4: Which keyword is used to define a generator function in Python?
- return
- yield (Correct answer)
- async
- generate
Correct answer: yield
Using `yield` instead of `return` makes a function a generator that produces values lazily.
Question 5: What does the `is` operator check in Python?
- Value equality
- Type equality
- Object identity (Correct answer)
- String equality
Correct answer: Object identity
`is` checks whether two variables refer to the exact same object in memory, not just equal values.
Question 6: What will `list(range(1, 10, 3))` produce?
- [1, 4, 7] (Correct answer)
- [1, 3, 6, 9]
- [1, 4, 7, 10]
- [3, 6, 9]
Correct answer: [1, 4, 7]
`range(1, 10, 3)` starts at 1, steps by 3, and stops before 10: 1, 4, 7.
Question 7: Which of the following is a mutable data type in Python?
- tuple
- str
- frozenset
- list (Correct answer)
Correct answer: list
Lists are mutable — their elements can be changed after creation, unlike tuples, strings, and frozensets.
What does the `*args` syntax in a function definition allow?