ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(x := 7, x * 2, [y := 3, y + 1])
- A. 7 14 [3, 3]
- B. 7 14 [3, 4]
- C. 7 14 NameError
- 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
- What is printed? python print('%s|%r' % ("it's", "it's"))
- What is printed? python print(ord('a') - ord('A'), chr(ord('A') + 32), 'Z'.lower() < 'a', 'z' > 'Z')
- What is printed? python print('-'.join('abc'), ''.join(map(str, [1, 2, 3])) == '123', ','.join([]) == '', 'x'.join(['', '']))
- What is printed? python s = 'hello' print(s.find('l'), s.index('l', 3), s.rfind('l'), s.count('ll'), s.find('z'))
- What is printed? python print('%.2e' % 0.000123456, '%G' % 1e-5, '%g' % 1e-5, '%.3g' % 1234.5)
- What is printed? python print(max(3, 3.0), min(3.0, 3), max([2, 2.0]), type(max(2.0, 2)).name)