nerdexam
Python_Institute

PCEP-30-02 · Question #69

What is the output of the following code? a = 1 b = 0 x = a or b y = not(a and b) print(x + y)

The correct answer is B. 2. Option B is correct because Python's or operator returns the first truthy value, so x = 1 or 0 yields 1. Then a and b evaluates to 0 (falsy), and not 0 returns True - which Python treats as the integer 1 in arithmetic, making x + y = 1 + 1 = 2. Option A is wrong because…

Question

What is the output of the following code? a = 1 b = 0 x = a or b y = not(a and b) print(x + y)

Options

  • AThe output cannot be predicted.
  • B2
  • CThe program will cause an error.
  • D1

How the community answered

(42 responses)
  • A
    17% (7)
  • B
    71% (30)
  • C
    5% (2)
  • D
    7% (3)

Explanation

Option B is correct because Python's or operator returns the first truthy value, so x = 1 or 0 yields 1. Then a and b evaluates to 0 (falsy), and not 0 returns True - which Python treats as the integer 1 in arithmetic, making x + y = 1 + 1 = 2.

Option A is wrong because Python's evaluation rules are fully deterministic - there is nothing unpredictable here. Option C is wrong because all operations (or, and, not, +) are valid on these types; no exception is raised. Option D (1) is the classic trap: students may assume not(a and b) produces False (i.e., 0), forgetting that not 0 is True, not False.

Memory tip: In Python, True and False are subclasses of int - always worth 1 and 0 respectively in arithmetic. Whenever you see a boolean in a + expression, substitute its integer equivalent before adding.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice