POC Core Python Programming Concepts 3 — Questions and Answers
Question 1: 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([])` evaluates to False.
Question 2: Which statement about Python's `with` statement is correct?
- It replaces try/except blocks entirely
- It automatically calls __enter__ and __exit__ on a context manager (Correct answer)
- It is only used for file I/O
- It creates a new variable scope
Correct answer: It automatically calls __enter__ and __exit__ on a context manager
The `with` statement invokes `__enter__` at the start and guarantees `__exit__` is called when the block ends, even if an exception occurs.
Question 3: What is the result of `'hello'[1:4]`?
- 'hel'
- 'ell' (Correct answer)
- 'ello'
- 'hell'
Correct answer: 'ell'
Slice notation [1:4] returns characters at indices 1, 2, and 3, which are 'e', 'l', 'l' → 'ell'.
Question 4: Which built-in function returns an iterator of (index, value) pairs?
- zip()
- map()
- enumerate() (Correct answer)
- filter()
Correct answer: enumerate()
`enumerate()` wraps an iterable and yields (index, value) tuples for each element.
Question 5: What happens when you raise an exception without an argument inside an `except` block?
- A new RuntimeError is created
- The caught exception is re-raised (Correct answer)
- Python ignores the raise statement
- A SyntaxError is thrown
Correct answer: The caught exception is re-raised
A bare `raise` inside an `except` block re-raises the currently handled exception with its original traceback.
Question 6: Which of the following is a mutable data type in Python?
- tuple
- frozenset
- str
- list (Correct answer)
Correct answer: list
Lists are mutable; their elements can be added, removed, or changed after creation, unlike tuples, frozensets, and strings.
Question 7: What is the output of `print(10 // 3)`?
- 3.33
- 3 (Correct answer)
- 4
- 1
Correct answer: 3
The `//` operator performs floor division, discarding the remainder and returning the integer quotient.
What is the output of `bool([])` in Python?