ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
x = 10
def f():
return x
x = 20
print(f(), (lambda y=x: y)(), [lambda: i for i in range(2)][1]())- A. 10 20 1
- B. 20 10 1
- C. 20 20 1
- D. 20 20 0
Correct answer: C. 20 20 1
f looks up x when it is called, so it sees 20. The default of y is taken when the lambda is created, which is after x becomes 20. The comprehension lambda returns the loop variable, which is 1 at the end of the loop.
More Python Programming questions
- What is printed? python print(list(map(int, '1 2 3'.split())), list(map(str.upper, 'ab')), sum(map(int, '9' 3)) % 7)
- What is printed? python print('%.3g' % 0.0001234, '%.3g' % 123456.0, '%g' % 1e-5, '%G' % 1e-10)
- What is printed? python print(max([1, 2], [1, 2, 0]) == [1, 2, 0], min((1, 'a'), (1, 'b')), sorted([(1, 'b'), (1, 'a')])[0][1])
- What is printed? python print(3 [0] == [0, 0, 0], [0] 3 is [0] 3, 'a' in 'abc' in 'abcdef')
- What is printed? python print(sum([1, 2, 3]) / 2 // 1, 7 // 2 / 2, divmod(7.0, 2)[0] == 7 // 2)
- What is printed? python print('{:^6}'.format('ab'), '{:<4}'.format(1), '{:>3}|{:<3}|'.format('a', 'b'))