ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def f(n):
return [i for i in range(n) if i % 3 == 0 if i % 2 == 0]
g = lambda n: [i for i in range(n) if i % 3 == 0 or i % 2 == 0]
print(f(13), len(g(13)))- A. [0, 6, 12] 7
- B. [0, 2, 3, 4, 6, 8, 9, 10, 12] 3
- C. [0, 6, 12] 9
- D. [0, 6] 9
Correct answer: C. [0, 6, 12] 9
Two consecutive if clauses act as `and`, so f keeps multiples of 6 below 13. For g, numbers 0..12 divisible by 2 or 3: 0, 2, 3, 4, 6, 8, 9, 10, 12, which is 9 numbers.
More Python Programming questions
- What is printed? python a = {"x": 1, "y": 2} b = {v: k for k, v in a.items()} c = {k: v for k, v in zip("abc", [1, 2])} print(b, c, {1…
- What is printed? python a = b = [] a.append(1) c, d = [], [] c.append(1) x = y = 5 x += 1 print(b, d, y)
- What is printed? python a = [1, 2, 3] i = 0 i, a[i] = 2, 9 print(a, i)
- What is printed? python x = [0, 1] i = 0 x[i], i = 5, 1 j = 1 j, x[j] = 0, 7 print(x, i, j)
- What is printed? python a, b = 1, 2 a, b = b, a + b c = [3, 4] c[0], c[1] = c[1], c[0] m, n = n, m = 7, 8 print(a, b, c, m, n)
- What is printed? python b = "Hé".encode("utf-8") print(len("Hé"), len(b), b[1], b.decode("latin-1"))