PCEP-30-02 · Question #251
The following code: print(float("1", "3"))
The correct answer is D. raises a ValueError exception. Option D is the closest correct answer because float() accepts only one argument, so passing two strings ("1" and "3") raises an exception - though technically a TypeError ("float() takes at most 1 argument (2 given)"), not a ValueError. A ValueError would occur if you passed a…
Question
Options
- Aprints 13
- Bprints 1,3
- Cprints 1, 3
- Draises a ValueError exception.
How the community answered
(29 responses)- A14% (4)
- B7% (2)
- C3% (1)
- D76% (22)
Explanation
Option D is the closest correct answer because float() accepts only one argument, so passing two strings ("1" and "3") raises an exception - though technically a TypeError ("float() takes at most 1 argument (2 given)"), not a ValueError. A ValueError would occur if you passed a single non-numeric string like float("abc"). Options A and B are wrong because float() never concatenates or joins its arguments - that's not how the function works. Option C is similarly wrong for the same reason; no amount of formatting produces 1, 3 from this call.
Memory tip: Think of float() like a single-seat car - it can carry exactly one passenger (a string or number). Trying to squeeze in two passengers causes a crash (exception) before it even moves.
Community Discussion
No community discussion yet for this question.