ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
s = {1, 2, 3}
s.add(2)
s.discard(9)
s.update([3, 4], (5,))
r = s.pop() if False else len(s)
try:
s.remove(9)
except KeyError:
print("KE", r)- A. KE 5
- B. KE 6
- C. 5
- D. KE 4
Correct answer: A. KE 5
Adding an existing element and discarding a missing one are no-ops; update accepts several iterables, giving {1, 2, 3, 4, 5}. remove (unlike discard) raises KeyError for a missing element.
More Python Programming questions
- What is printed? python d = {"a": 1, "b": 2} items = d.items() print(("a", 1) in items, ("a", 2) in items, items & {("b", 2), ("c", 3)} ==…
- What is printed? python d = dict(a=1) d2 = d d = {d, "b": 2} d2["c"] = 3 del d2["a"] print(d, d2, dict([("x", 1), ("x", 2)])…
- What is printed? python def f(a, b=[]): b = b or [] b.append(a) return b x = f(1) y = f(2, []) z = f(3, [0]) print(x, y, z, f.defaults)
- What is printed? python def f(x=[]): x += [len(x)] return x def g(x=()): x += (len(x),) return x print(f(), f(), g(), g())
- What is printed? python funcs = {} for name in ["inc", "dbl"]: if name == "inc": def op(v): return v + 1 else: def op(v): return v 2…
- What is printed? python gen = (x for x in range(3)) lst = [x for x in range(3)] print(type(gen).name, len(lst), sum(gen), sum(gen)…