Data Structures & AlgorithmsModerateTCS NQT · basics

Problem: How many integers in [1, 50] are divisible by both 2 and 3? Run the code.

Problem: How many integers in [1, 50] are divisible by both 2 and 3? Run the code.
print(sum(1 for x in range(1, 51) if x % 2 == 0 and x % 3 == 0))
What is printed?
  1. A. 16
  2. B. 8
  3. C. 25
  4. D. 6

Correct answer: B. 8

Being divisible by both 2 and 3 means divisible by 6, giving 6, 12, ..., 48 which is 8 numbers.

More Coding Interview Problems questions

Exams that ask this topic