ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(1 if 0 else 2 if 1 else 3, (lambda: 1 if False else 'x')())
  1. A. 1 x
  2. B. 3 x
  3. C. 2 x
  4. D. 2 1

Correct answer: C. 2 x

Conditional expressions nest to the right, so 0 is false and the second branch gives 2. The lambda's inner conditional is false and returns 'x'.

More Python Programming questions

Exams that ask this topic