ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
x = [1, 2, 3] y = x.copy() y.append(4) print(len(x), len(y), x is y)
- A. 3 4 True
- B. 4 4 False
- C. 3 4 False
- D. 3 3 False
Correct answer: C. 3 4 False
copy makes a new list, so appending to y does not change x. The two names refer to different objects.
More Python Programming questions
- What is printed? python print('aaa' < 'b', 'abc' > 'abd')
- What is printed? python print(sum(1 for c in 'hello world' if c in 'aeiou'))
- What is printed? python print(list(filter(lambda x: x > 0, map(lambda x: x - 2, [1, 2, 3, 4]))))
- What is printed? python print(1e3, 1000.0 == 1e3, 10 -2)
- What is printed? python print(type(True + 1).name, type(2 1.0).name)
- What is printed? python print(list(enumerate(['x'], 1))[0][0] + 1)