PCEP-30-02 practice question 11 of 35
We need to check if all elements of the iterable object evaluate to the truth. What built-in function can you use for this?
Full set: 548 questionsDomain Computer Programming and Python Fundamentals
The built-in function all(iterable) is designed specifically for this purpose. It takes one argument, an iterable (like a list or tuple), and returns True if all elements within that iterable evaluate to a Boolean True. If the iterable is empty, it also returns True. It returns False as soon as it encounters the first element that evaluates to False, providing an efficient check. This directly matches the requirement to verify that all elements of an iterable object are truthy.
A. any() returns True if at least one element of the iterable is true, not all of them.
C. bin() is unrelated; it converts an integer into its binary string representation.
D. bool() converts a single specified object to its Boolean value (True or False), but it does not operate on an entire iterable. ---
1. Python Software Foundation. (2023). Built-in Functions — The Python Standard Library. Python 3.12.0 documentation.
Section: all(iterable)
Details: "Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: def all(iterable): for element in iterable: if not element: return False return True"
URL: https://docs.python.org/3/library/functions.html#all
2. Python Software Foundation. (2023). Truth Value Testing — The Python Standard Library. Python 3.12.0 documentation.
Section: Built-in Types
Details: This section explains which values are considered false in Python (e.g., None, False, zero of any numeric type, empty sequences and collections). The all() function uses these rules for evaluation.
URL: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
3. Guttag, J. V. (2016). Introduction to Computation and Programming Using Python, With Application to Understanding Data (2nd ed.). MIT Press.
Chapter 5: Structured Types, Mutability, and Higher-Order Functions
Details: University-level texts on Python programming introduce built-in functions that operate on iterables. The all() function is a standard example of a higher-order function that aggregates a result from a sequence based on a logical condition (truthiness).
Keep practicing
Take the free PCEP-30-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCEP-30-02 practice questions
- Question 1The following list is given:
- Question 2What is the output of the following code? num = 10 while num >= 0: num -= 2 print(num)
- Question 4We need to check if at least one element of the iterable object evaluates to the truth. W…
- Question 5This question is a tricky one. What is the result of the following code snippet? numbers …
- Question 8What is the purpose of the continue statement in Python?
- Question 12What is the expected output of the following code? x = '\'' print(len(x))
