PCEP-30-02 · Question #285
What is the expected output of the following code? x = 1 if x > 0 or x < 1: print("1") if x > 1: print("2") elif x >= 1: print("3") else: print("4")
The correct answer is B. 1 | 1 2 | 3. Option B is correct because x = 1 makes the first condition x > 0 or x < 1 evaluate to True (since 1 > 0 is True, the or short-circuits and prints "1"), and in the second block, x > 1 is False but x >= 1 is True, so "3" prints - giving output 1 then 3. Why the distractors fail…
Question
Options
- A1 | 1 2 | 2 3 | 4
- B1 | 1 2 | 3
- C1 | 2
- D1 | 1 2 | 4
How the community answered
(18 responses)- A6% (1)
- B78% (14)
- C11% (2)
- D6% (1)
Explanation
Option B is correct because x = 1 makes the first condition x > 0 or x < 1 evaluate to True (since 1 > 0 is True, the or short-circuits and prints "1"), and in the second block, x > 1 is False but x >= 1 is True, so "3" prints - giving output 1 then 3.
Why the distractors fail:
- A includes
"2"and"4", butx > 1isFalse(1 is not greater than 1), so"2"never prints, and onceelifmatches,elseis skipped entirely. - C prints only
"2", which would require the firstifto beFalseandx > 1to beTrue- neither is the case whenx = 1. - D includes
"4", butelseonly runs when bothifandelifareFalse; sincex >= 1catchesx = 1,elseis skipped.
Memory tip: Remember that >= means "greater than OR equal to," so it catches the boundary value (x = 1) that > misses - and in an if/elif/else chain, only the first true branch executes, making the rest unreachable.
Community Discussion
No community discussion yet for this question.