ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
x = [1, 2, 3] y = x[:] y[0] = 9 z = x z[1] = 8 print(x)
- A. [9, 2, 3]
- B. [1, 8, 3]
- C. [1, 2, 3]
- D. [9, 8, 3]
Correct answer: B. [1, 8, 3]
x[:] makes a (shallow) copy, so changing y does not touch x. z = x is an alias, so z[1] = 8 changes x. Result: [1, 8, 3].
More Python Programming questions
- What is printed? python print(sorted([3, -1, 2, -5], key=abs, reverse=True))
- What is printed? python print(list(filter(None, [0, 1, "", "a", [], [0], None])))
- What is printed? python def gen(): yield 1 return 5 yield 2 print(list(gen()))
- What is printed? python print("a,b,,c".split(","), len("a b c".split()))
- What is printed? python print(True + True 3, type(True + 1).name)
- What is printed? python a = [1, 2, 3, 4, 5] a[1:3] = [9] a[::2] = [0, 0] print(a)