ProgrammingModerateAccenture pattern
What is printed?
What is printed?
print({x % 3 for x in range(10)})- A. {0, 1, 2}
- B. {0, 1, 2, 3}
- C. {1, 2, 3}
- D. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Correct answer: A. {0, 1, 2}
x % 3 for x in 0..9 can only ever be 0, 1 or 2, and a set keeps each distinct result once, giving {0, 1, 2} regardless of how many x values produced each remainder.
More Python Programming questions
- What is printed? python def countdown(n): while n > 0: yield n n -= 1 g = countdown(3) print(list(g)) print(list(g))
- What is printed? python it = iter([1, 2, 3]) print(next(it), next(it)) for x in it: print("loop", x) print(list(it))
- What is printed? python def flatten(lst): for item in lst: if isinstance(item, list): yield from flatten(item) else: yield item…
- What is printed? python g = (x for x in range(3)) print(sum(g), sum(g))
- What is printed? python def gen5(): for i in range(5): if i == 3: return yield i print(list(gen5()))
- What is printed? python def gen7(): try: yield 1 yield 2 finally: print("cleanup") g = gen7() print(next(g)) g.close()