PCEP-30-02 · Question #102
Consider the following code. x = oat ('23 42') Which of the following expressions will evaluate to 2?
The correct answer is B. bool(x) + True. x = float('23.42') (the fl is cut off by the question's formatting), so x holds the float value 23.42. Option B is correct because bool(x) evaluates to True (any non-zero number is truthy), and in Python, True has an integer value of 1. So bool(x) + True becomes 1 + 1 = 2. Why…
Question
Options
- Aint(x) + False
- Bbool(x) + True
- Cstr(x)
- Dbool(x)
How the community answered
(38 responses)- A16% (6)
- B71% (27)
- C8% (3)
- D5% (2)
Explanation
x = float('23.42') (the fl is cut off by the question's formatting), so x holds the float value 23.42.
Option B is correct because bool(x) evaluates to True (any non-zero number is truthy), and in Python, True has an integer value of 1. So bool(x) + True becomes 1 + 1 = 2.
Why the distractors fail:
- A -
int(x) + Falsetruncates23.42to23, then adds0(False = 0), giving23, not2. - C -
str(x)returns the string'23.42', which is text, not an integer. - D -
bool(x)alone returnsTrue, which equals1, not2.
Memory tip: Remember that in Python, True == 1 and False == 0, so booleans participate in arithmetic. When you see bool + bool or bool + int, mentally substitute 1 or 0 and do the math - True + True is always 2.
Community Discussion
No community discussion yet for this question.