nerdexam
Python_Institute

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

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)

Options

  • ATrue True
  • BTrue False
  • CFalse False
  • DFalse True

How the community answered

(17 responses)
  • A
    6% (1)
  • B
    71% (12)
  • C
    18% (3)
  • D
    6% (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:

LineOperationResult
x = True, y = Falseinitializationx=True, y=False
x = x or yTrue or Falsex=True
x = x and yTrue and Falsex=False
x = x or yFalse or Falsex=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): y is never changed, so it can never print True.
  • B (True False): This would require x to remain True after the and operation, but True and False = False.
  • D (False True): Again, y is 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.

Full PCEP-30-02 Practice