nerdexam
Python_Institute

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

Consider the following code. x = oat ('23 42') Which of the following expressions will evaluate to 2?

Options

  • Aint(x) + False
  • Bbool(x) + True
  • Cstr(x)
  • Dbool(x)

How the community answered

(38 responses)
  • A
    16% (6)
  • B
    71% (27)
  • C
    8% (3)
  • D
    5% (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) + False truncates 23.42 to 23, then adds 0 (False = 0), giving 23, not 2.
  • C - str(x) returns the string '23.42', which is text, not an integer.
  • D - bool(x) alone returns True, which equals 1, not 2.

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.

Full PCEP-30-02 Practice