ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
x = [0] * 3 x[0] += 1 y = [[0]] * 2 y[0][0] += 1 print(x, y)
- A. [1, 0, 0] [[0], [0]]
- B. [0, 0, 0] [[1], [1]]
- C. [1, 1, 1] [[1], [1]]
- D. [1, 0, 0] [[1], [1]]
Correct answer: D. [1, 0, 0] [[1], [1]]
[0] * 3 makes three separate ints, so only the first one changes. [[0]] * 2 repeats one inner list twice, so the update to y[0][0] shows through both references.
More Python Programming questions
- What is printed? python print(int(float('1e16')) + 1, float(int(260) + 1) == float(260))
- What is printed? python print('a-bc d'.split('-'), 'a-bc d'.replace('-', ' ').split(''), 'a-b'.partition('-')[0])
- What is printed? python print(sum([1] 105) == 105, sum([0.5] 105) == 5e4, 1/3 + 1/3 + 1/3 == 1.0, 0.7 + 0.2 + 0.1 == 1.0)
- What is printed? python from fractions import Fraction print(Fraction(7, 3) > 2, float(Fraction(7, 3)), Fraction(7, 3) // 1, Fraction(-7…
- What is printed? python print(1 + 2 3 2 // 4 % 5, (1 + 2) 3 2 // 4 % 5, -2 2, (-2) 2)
- What is printed? python print(max('zebra', 'Zebra'), min('zebra', 'Zebra'), 'apple' > 'Apple', 'Z' < 'a')