PCEP-30-02 · Question #169
What is the expected output of the following code? ``python def fun(): return True x = fun(false) print(x) ``
The correct answer is C. The program will cause an error. Option C is correct because Python's boolean literals are case-sensitive - the valid keyword is False (capitalized), not false. When Python tries to evaluate fun(false), it raises a NameError: name 'false' is not defined before the function even runs. And even if false were…
Question
def fun():
return True
x = fun(false)
print(x)
Options
- A0
- BFalse
- CThe program will cause an error.
- DTrue
- E1
How the community answered
(33 responses)- A12% (4)
- C79% (26)
- D3% (1)
- E6% (2)
Explanation
Option C is correct because Python's boolean literals are case-sensitive - the valid keyword is False (capitalized), not false. When Python tries to evaluate fun(false), it raises a NameError: name 'false' is not defined before the function even runs. And even if false were corrected to False, a second error would occur: fun() is defined with no parameters, so passing any argument would raise a TypeError.
Why the distractors are wrong:
- D (True): The function does contain
return True, so this feels plausible - but execution never reaches the function body because the error halts the program first. - B (False): There is no path to
Falsehere; the function returnsTrue, notFalse, making this doubly wrong. - A (0) and E (1): These exploit the fact that
False == 0andTrue == 1in Python, tempting those who know booleans are integers - but again, the error prevents any output.
Memory tip: In Python, always capitalize booleans - True and False - just like you capitalize proper nouns in English. Lowercase true/false are JavaScript conventions; using them in Python always triggers a NameError.
Community Discussion
No community discussion yet for this question.