ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
def is_prime(n):
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
break
i += 1
else:
return True
return False
print([n for n in range(2, 20) if is_prime(n)])- A. [2, 3, 5, 7, 9, 11, 13, 15, 17, 19]
- B. [3, 5, 7, 11, 13, 17, 19]
- C. [2, 3, 5, 7, 11, 13, 17, 19]
- D. [2, 3, 5, 7, 11, 13, 17]
Correct answer: C. [2, 3, 5, 7, 11, 13, 17, 19]
The while-else clause runs only when the loop ends without a break, so the function returns True only when no divisor was found. For n = 2 and 3 the loop never starts, and the else branch still runs.
More Python Programming questions
- For a Python list a, which operation takes time proportional to j - i?
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(n, last): if n == 0: return 1 total = f(n - 1, 0) if…
- What is printed? python a = [1, 2, 4] cnt = 0 for mask in range(1 << len(a)): s = sum(a[i] for i in range(len(a)) if mask >> i & 1) if s %…
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} d['a'] = 9 del d['b'] d['b'] = 5 print(list(d.items()))
- What is printed? python import bisect a = [(1, 'x'), (3, 'y'), (3, 'z'), (7, 'w')] i = bisect.bisectleft(a, 3, key=lambda p: p[0]) j =…
- Which statement about counting pairs i < j with a[i] + a[j] == t is FALSE?