ProgrammingModerateInfosys · Accenture · Capgemini pattern

What is printed?

What is printed?
print(-2 ** 2, (-2) ** 2, 2 ** -1)
  1. A. 4 4 0.5
  2. B. 4 -4 0.5
  3. C. -4 4 0.5
  4. D. -4 4 0

Correct answer: C. -4 4 0.5

** binds tighter than unary minus, so -2 ** 2 = -(2 ** 2) = -4. With brackets, (-2) ** 2 = 4. A negative exponent always gives a float: 2 ** -1 = 0.5.

More Python Programming questions

Exams that ask this topic