ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = 0
def f():
global x
x += 1
return x
print(f() + f() * 10, x)- A. 12 2
- B. 21 1
- C. 21 2
- D. 11 1
Correct answer: C. 21 2
Python evaluates the left operand f() first (x becomes 1, value 1), then f() * 10 (x becomes 2, value 2 * 10 = 20). The sum is 21, and the global is 2.
More Python Programming questions
- What is printed? python print(0.1 + 0.2 == 0.3, round(0.1 + 0.2, 10) == 0.3, 3 0.1 == 0.3 - 0.2 + 0.1)
- What is printed? python nums = [3, 1, 2] nums2 = sorted(nums, reverse=True) nums.sort() print(nums, nums2, nums is sorted(nums))
- What is printed? python nums = [13, 22, 5, 31, 8, 3] print(sorted(nums, key=lambda x: x % 10), sorted(nums, key=lambda x: x % 10…
- What is printed? python def gen(): yield 1 return 5 yield 2 it = gen() print(list(it), next(it, 'done'))
- What is printed? python from itertools import accumulate print(list(accumulate([2, 3, 4], lambda a, b: a b)), list(accumulate([1, -1, 2])))
- What is printed? python a = [[1, 2], [3, 4]] b = [row[:] for row in a] c = a[:] a[0][0] = 9 c[1][1] = 7 print(b, c)