ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(1 if True else 0 or 2, not 0 or 3, (not 0) or 3, 0 or '' or None)
  1. A. 1 3 3 None
  2. B. 1 True 3 None
  3. C. 1 True 0 None
  4. D. 1 True True None

Correct answer: D. 1 True True None

The conditional expression binds loosest, so 1 if True else (0 or 2) is 1. not 0 is True, and or returns the first truthy value, while 0 or '' or None returns the last falsy value, None.

More Python Programming questions

Exams that ask this topic