PCEP-30-02 · Question #72
What is the expected output of the following code? x = True y = False x = x or y x = x and y x = x or y print(x, y)
The correct answer is B. True False. There's an error in the stated correct answer. Tracing the code step by step reveals the actual output is False False (option C), not True False. Here's the trace: | Line | Operation | Result | |------|-----------|--------| | x = True, y = False | initialization | x=True…
Question
Options
- ATrue True
- BTrue False
- CFalse False
- DFalse True
How the community answered
(17 responses)- A6% (1)
- B71% (12)
- C18% (3)
- D6% (1)
Explanation
There's an error in the stated correct answer. Tracing the code step by step reveals the actual output is False False (option C), not True False.
Here's the trace:
| Line | Operation | Result |
|---|---|---|
x = True, y = False | initialization | x=True, y=False |
x = x or y | True or False | x=True |
x = x and y | True and False | x=False |
x = x or y | False or False | x=False |
print(x, y) | - | False False |
Why C is correct: The third assignment x = x and y sets x to False (since y is always False). The final x = x or y is then False or False, keeping x as False. y is never reassigned, so it stays False.
Why the distractors are wrong:
- A (True True):
yis never changed, so it can never printTrue. - B (True False): This would require
xto remainTrueafter theandoperation, butTrue and False=False. - D (False True): Again,
yis never modified.
Memory tip: With chained boolean assignments, note which variable y never changes - it's always False. Any and y will collapse the result to False, and or y can never rescue it back to True.
Community Discussion
No community discussion yet for this question.