ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
s = "abc" it = iter(s) print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
- A. a abc [] ['a', 'b']
- B. a bc ['b', 'c'] ['a', 'b']
- C. a bc [] ['a', 'b', 'z']
- D. a bc [] ['a', 'b']
Correct answer: D. a bc [] ['a', 'b']
join consumes the rest of the iterator after next() took 'a', so list(it) is then empty. The comprehension filters the original string.
More Python Programming questions
- What is printed? python def f(n): if n == 0: return [] return [n] + f(n - 1) if n % 2 else f(n - 1) + [n] print(f(5))
- What is printed? python x = 1 def f(): print(x, end=" ") def g(): x = 2 f() g() h = lambda: x x = 3 print(h())
- What is printed? python def outer(): v = "a" def inner(): return v v = "b" r1 = inner() del v try: return r1, inner() except NameError as…
- What is printed? python print(eval("[i for i in range(3)]"), [x for x in "ab"] 2, list(map(str.upper, "ab")), {"aba"} == {"a", "b"})
- What is printed? python class A: def init(self): self.v = 1 def eq(self, o): return isinstance(o, A) and self.v == o.v hash = None class…
- What is printed? python print(isinstance(True, int), issubclass(bool, int), type(True) is int, True == 1, hash(True) == hash(1.0))