ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def nums():
for n in range(5):
print(n, end="")
yield n
r = any(n > 1 for n in nums())
print("", r)- A. 01234 True
- B. 012 True
- C. 2 True
- D. 012 2
Correct answer: B. 012 True
any() short-circuits: it pulls items only until one is truthy (n = 2). The generator prints 0, 1 and 2 and is never advanced further.
More Python Programming questions
- What is printed? python gen = (i for i in range(3)) print(1 in gen, 1 in gen, list(gen))
- What is printed? python x = 10 class K: x = 20 y = [x + i for i in range(2)] z = (lambda: x)() print(K.y, K.z)
- What is printed? python class K: n = 3 a = [i for i in range(n)] try: b = [n for i in range(2)] except NameError: b = "NE" print(K.a, K.b)
- What is printed? python total = 0 squares = [total := total + v for v in [1, 2, 3]] print(squares, total)
- What is printed? python print([[r c for c in range(r)] for r in range(1, 4)])
- What is printed? python pairs = [(i, j) for i in range(3) if i for j in range(i) if j != 1] print(pairs)