ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(len([x for x in range(1, 100) if x % 3 == 0 or x % 5 == 0]), sum(x for x in range(1, 100) if x % 3 == 0 or x % 5 == 0))
  1. A. 46 2233
  2. B. 45 2318
  3. C. 47 2318
  4. D. 46 2318

Correct answer: D. 46 2318

There are 33 multiples of 3, 19 of 5 and 6 of 15 below 100, so 33 + 19 - 6 = 46. Their sum is 1683 + 950 - 315 = 2318, using inclusion-exclusion for the overlap.

More Python Programming questions

Exams that ask this topic