Built-ins & Standard Library · Question 17 · Hard
Booleans are used in an isinstance check, in addition and inside sum. What does this code print?
flags = [x > 2 for x in [1, 3, 5]]
print(isinstance(True, int), True + True, sum(flags))
- False 2 2
- True True 2
- True 2 2
- True 2 3
Answer and explanation
Answer: C. True 2 2
Explanation: bool is a subclass of int, with True equal to 1 and False equal to 0. So True + True is 2, and summing [False, True, True] counts the two True values.
Previous: itertools.groupby is applied directly to a string in which one letter appears… · Next: A program imports the same module helper from two different files. How many…
Practise built-ins & standard library
Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.
More built-ins & standard library questions
- itertools is used to count the 2-item combinations and the 2-item permutations…
- sorted is given a key that returns a tuple, to rank students by marks (high to…
- itertools.groupby is applied directly to a string in which one letter appears…
- A program imports the same module helper from two different files. How many…
- A defaultdict(int) is updated for one key, read for a second key and tested…
- all, any and max are each given an empty list, with a default supplied to max…
More python practice
Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge