Data Structures & AlgorithmsModerateTCS NQT · Product Company OA

Problem: How many integers in [1, 30] are even but not divisible by 4? Run the code.

Problem: How many integers in [1, 30] are even but not divisible by 4? Run the code.
print(sum(1 for x in range(1, 31) if x % 2 == 0 and x % 4 != 0))
What is printed?
  1. A. 7
  2. B. 15
  3. C. 8
  4. D. 9

Correct answer: C. 8

There are 15 even numbers and 7 multiples of 4 in [1, 30], so 8 remain.

More Coding Interview Problems questions

Exams that ask this topic