1. What is the output of the following Python code?
x = [0]
print(bool(x))
A) True
B) False
C) 0
D) [0]
Answer: A) True
(A list containing elements, even 0, is non-empty and evaluates to True.)
2. What is the output of the following expression?
print([] or "Python")
A) []
B) False
C) "Python"
D) True
Answer: C) "Python"
(The or operator returns the first truthy operand, or the last operand if all are falsy.)
3. What does the following snippet print?
a = []
b = [1, 2]
print(a and b)
A) False
B) []
C) [1, 2]
D) None
Answer: B) []
(The and operator short-circuits on the first falsy value and returns the object itself, not a boolean.)
4. What is the evaluated output of this code?
x = [[]]
print(not x)
A) True
B) False
C) [[]]
D) Error
Answer: B) False
(x contains one element—an empty list—so x itself is non-empty and truthy. not x yields False.)
5. Which of the following expressions evaluates to True?
A) bool([])
B) bool("")
C) bool([False])
D) bool(0)
Answer: C) bool([False])
(A list with one element [False] has a length of 1, making it truthy.)
6. What does this code snippet print?
print(not [] == True)
A) True
B) False
C) None
D) SyntaxError
Answer: B) False
(Operator chaining applies here: not ([] == True). Since [] == True evaluates to False, the expression parses as not ([] == True) which produces False due to operator precedence where == binds tighter than not.)
7. What will be printed by the following expression?
print([1] and [] or [2])
A) [1]
B) []
C) [2]
D) True
Answer: C) [2]
([1] and [] evaluates to []. Then [] or [2] evaluates to [2].)
8. What is the output of the following condition?
items = []
result = "Non-empty" if items else "Empty"
print(result)
A) "Non-empty"
B) "Empty"
C) None
D) []
Answer: B) "Empty"
(An empty list evaluates to falsy in a ternary/conditional expression.)
9. What does the following comparison print?
print([] == False)
A) True
B) False
C) None
D) Error
Answer: B) False
(Falsiness does not mean equality. [] is a list, while False is a boolean; they are distinct objects with different types.)
10. What is the output of running this code?
x = []
print(not not x)
A) True
B) False
C) []
D) N
Answer: B) False
(not x evaluates to True, and the second not inverts it back to False.)
No comments:
Post a Comment