What is the expected output of evaluating the following Python expression?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What is the expected output of evaluating the following Python expression?
all([2, 56, 4, 0, 5, 12])
Show answer & explanation
Answer: A. False
The all() function returns True only if all elements in the iterable are truthy. In this list, the integer 0 is falsy, so all() immediately returns False. The remaining elements (2, 56, 4, 5, 12) are truthy, but the presence of even one falsy value causes the entire expression to evaluate to False.
Step-by-step Derivation:
Step-by-step evaluation of all([2, 56, 4, 0, 5, 12]):
- all() checks each element for truthiness: 2 (truthy), 56 (truthy), 4 (truthy), 0 (falsy) ← stops here
- Since 0 is falsy and all() requires ALL elements to be truthy, it returns False
- Output: False (a boolean value, not the integer 0)
Note: Option D (0) is incorrect because all() always returns a boolean (True or False), never an integer.