ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 5
print(x if x > 3 else 0 if x == 3 else -1, 2 ** -1, 7 % 3 ** 2)
  1. A. 5 0.5 7
  2. B. 5 0.5 1
  3. C. 5 0 7
  4. D. 5 0.5 3

Correct answer: A. 5 0.5 7

Conditional expressions nest to the right, and x = 5 satisfies the first test. ** binds tighter than %, so 3 ** 2 = 9 and 7 % 9 is 7. A negative exponent on an int gives a float, 0.5.

More Python Programming questions

Exams that ask this topic