ProgrammingModerateInfosys · Python
What is printed?
What is printed?
d = {'a': 1, 'b': 2}
d['c'] = 3
del d['a']
d['a'] = 9
print(list(d.items()))- A. [('a', 9), ('b', 2), ('c', 3)]
- B. [('b', 2), ('c', 3), ('a', 9)]
- C. [('b', 2), ('c', 3)]
- D. [('a', 1), ('b', 2), ('c', 3)]
Correct answer: B. [('b', 2), ('c', 3), ('a', 9)]
Dicts keep insertion order; deleting 'a' and inserting it again places it at the end.
More Python Programming questions
- 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))
- What is printed? python print(7 // 2, -7 // 2, 7 % -3, divmod(-7, 2), 2 -1)