nerdexam
Python_Institute

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

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")

Options

  • A1 | 1 2 | 2 3 | 4
  • B1 | 1 2 | 3
  • C1 | 2
  • D1 | 1 2 | 4

How the community answered

(18 responses)
  • A
    6% (1)
  • B
    78% (14)
  • C
    11% (2)
  • D
    6% (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", but x > 1 is False (1 is not greater than 1), so "2" never prints, and once elif matches, else is skipped entirely.
  • C prints only "2", which would require the first if to be False and x > 1 to be True - neither is the case when x = 1.
  • D includes "4", but else only runs when both if and elif are False; since x >= 1 catches x = 1, else is 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.

Full PCEP-30-02 Practice