ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
print(x := 7, x * 2, [y := 3, y + 1])
  1. A. 7 14 [3, 3]
  2. B. 7 14 [3, 4]
  3. C. 7 14 NameError
  4. D. 7 7 [3, 4]

Correct answer: B. 7 14 [3, 4]

The walrus operator := assigns inside an expression, so x is set before x * 2 is evaluated, and y is set before y + 1 inside the list display. Evaluation runs left to right.

More Python Programming questions

Exams that ask this topic