ProgrammingVery hardProduct company OA
What is printed?
What is printed?
d = {"b": 1, "a": 2, "c": 3}
d["a"] = 9
d.update(b=7, d=4)
d["c"] = d.pop("c")
d.setdefault("a", 0)
e = {**d, "b": 0}
print(list(d), list(e), next(reversed(e)), d == {"a": 9, "b": 7, "c": 3, "d": 4})- A. ['b', 'a', 'd', 'c'] ['b', 'a', 'd', 'c'] c True
- B. ['b', 'a', 'c', 'd'] ['b', 'a', 'c', 'd'] d True
- C. ['b', 'a', 'd', 'c'] ['a', 'd', 'c', 'b'] b True
- D. ['b', 'a', 'd', 'c'] ['b', 'a', 'd', 'c'] c False
Correct answer: A. ['b', 'a', 'd', 'c'] ['b', 'a', 'd', 'c'] c True
A dict remembers the order in which keys were FIRST inserted; overwriting a value (d['a'] = 9, update(b=7), {**d, 'b': 0}) never moves a key. Only removing and re-adding does: pop('c') followed by assignment sends 'c' to the end, after the newly added 'd'. So both dicts list b, a, d, c and the last key of e is 'c'. Equality of dicts ignores order, hence True. The option that moves 'b' to the end of e assumes a repeated key in a dict display is re-inserted.
More Python Programming questions
- What is printed? python a = [1, 2]; b = a; c = a[:] t = (a, c) a += [3] b = b + [4] c = 2 print(t[0] is a, b is a, t[1] is c, len(t[0])…
- What is printed? python a = list(range(8)) b = a[::2] a[1::2] = b[::-1] a[2:5] = [] del a[-2:] b[0] = 99 print(a, b)
- What is printed? python class Meter: unit = "m" def init(self): self.dict["v"] = 1 def getattr(self, name): return name.upper() def…
- What is printed? python def counter(): n = 0 def inc(k=1): nonlocal n n += k return n return inc c1, c2 = counter(), counter() g = (c1(i)…
- What is printed? python def f(): out = [] for i in range(4): try: if i == 1: continue if i == 2: raise KeyError(i) if i == 3: return out…
- What is printed? python rows = [("x", 2), ("y", 1), ("z", 2), ("w", 1), ("v", 3)] a = sorted(rows, key=lambda r: r[1], reverse=True) b =…