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)))
  1. A. [0, 6, 12] 7
  2. B. [0, 2, 3, 4, 6, 8, 9, 10, 12] 3
  3. C. [0, 6, 12] 9
  4. 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

Exams that ask this topic