ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
s = set()
s.add((1, 2))
s.add((2, 1))
try:
s.add([1])
except TypeError:
s.add(frozenset([1]))
s.add(frozenset({1}))
print(len(s), (1, 2) in s, {1} in s)- A. 4 True False
- B. 3 True True
- C. 2 True True
- D. 3 True False
Correct answer: B. 3 True True
Tuples are ordered, so (1, 2) and (2, 1) are different. Lists are unhashable, so a frozenset is added instead; the second equal frozenset is a duplicate. A set can be looked up in a set via its frozenset equivalent.
More Python Programming questions
- What is printed? python d = {"a": 1, "b": 2, "c": 3} del d["a"] d["a"] = 4 d.pop("b") d["b"] = 5 print(list(d.items()), next(iter(d))…
- What is printed? python class Key: def init(self, v): self.v = v def hash(self): return hash(self.v) def eq(self, o): return self.v == o.v…
- What is printed? python d = dict.fromkeys(range(3), 0) for k in d: d[k] += k e = {k: [] for k in "ab"} e["a"].append(1) f =…
- What is printed? python x = None print(x is None, x == None, bool(None), type(None).name, None is not False)
- What is printed? python class P: pass p = P() p.n = 1 p.n += 1 P.m = 10 q = P() q.m += 5 print(p.n, P.m, q.m, p.m, vars(q))
- What is printed? python def makeacc(): total = [] def add(x): total.append(x) return sum(total) def reset(): nonlocal total total = []…