ProgrammingModerateTCS NQT · Python
What is printed?
What is printed?
a = [1, 2, 3] b = a b += [4] c = a c = c + [5] print(a, c)
- A. [1, 2, 3] [1, 2, 3, 5]
- B. [1, 2, 3, 4] [1, 2, 3, 5]
- C. [1, 2, 3] [1, 2, 3, 4, 5]
- D. [1, 2, 3, 4] [1, 2, 3, 4, 5]
Correct answer: D. [1, 2, 3, 4] [1, 2, 3, 4, 5]
`+=` extends the shared list in place, so `a` sees 4; `c + [5]` builds a new list bound to `c`, leaving `a` unchanged.
More Python Programming questions
- What is printed? python d = {'a': 1, 'b': 2} d['c'] = 3 del d['a'] d['a'] = 9 print(list(d.items()))
- What is printed? python s = "a,b,,c" print(s.split(','), s.split(',', 1), " x y ".split())
- What is printed? python print(list(zip('abc', range(5))), dict(zip('ab', [1, 2, 3])))
- What is printed? python def f(x, acc=[]): acc.append(x) return len(acc) print(f(1), f(2), f(3, []), f(4))
- What is printed? python print(all([]), any([]), all([0]), any([[]]), any([[0]]))
- What is printed? python w = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(w, key=len), sorted(w, key=len, reverse=True))