Python Core Syntax 4 — Questions and Answers
Question 1: What is the output of `print(not not True)`?
- False
- True (Correct answer)
- None
- SyntaxError
Correct answer: True
Applying `not` twice negates the value twice, returning the original `True`.
Question 2: Which of the following creates an empty dictionary in Python?
- {} (Correct answer)
- []
- ()
- set()
Correct answer: {}
An empty pair of curly braces `{}` creates an empty dictionary; `set()` must be used for an empty set.
Question 3: What does `'hello'[1:4]` evaluate to?
- 'hel'
- 'ell' (Correct answer)
- 'ello'
- 'hell'
Correct answer: 'ell'
Slice `[1:4]` extracts characters at indices 1, 2, and 3 (stop index is exclusive), yielding `'ell'`.
Question 4: In Python, what does the `global` keyword do inside a function?
- Creates a new global variable
- Imports a module globally
- Allows modification of a variable defined in the global scope (Correct answer)
- Makes the function accessible globally
Correct answer: Allows modification of a variable defined in the global scope
The `global` keyword tells Python that a name refers to the global scope, allowing assignment to that global variable inside a function.
Question 5: What is the result of `list(range(2, 10, 3))`?
- [2, 5, 8] (Correct answer)
- [2, 4, 6, 8]
- [2, 3, 4, 5, 6, 7, 8, 9]
- [2, 5, 8, 11]
Correct answer: [2, 5, 8]
`range(2, 10, 3)` starts at 2 and steps by 3, producing 2, 5, 8 (stops before 10).
Question 6: Which of the following correctly checks if a key exists in a dictionary `d`?
- d.has_key('k')
- 'k' in d (Correct answer)
- 'k' in d.values()
- d.contains('k')
Correct answer: 'k' in d
The `in` operator checks for key membership in a dictionary; `has_key()` was removed in Python 3.
Question 7: What is the difference between `is` and `==` in Python?
- `is` compares values; `==` compares types
- `is` checks identity (same object); `==` checks equality (same value) (Correct answer)
- They are identical in behavior
- `is` works only for numbers; `==` works for all types
Correct answer: `is` checks identity (same object); `==` checks equality (same value)
`is` tests whether two variables point to the exact same object in memory, while `==` tests whether their values are equal.
What is the output of `print(not not True)`?