ProgrammingVery hardCAT · MBA placement

Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop. What is printed?

Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop. What is printed?
count = 0
for n = 1 to 1000
    if n mod 7 == 0 and n mod 11 != 0 then count = count + 1
end for
Print count
  1. A. 130
  2. B. 922
  3. C. 142
  4. D. 12

Correct answer: A. 130

Multiples of 7 up to 1000 number 142, and 12 of them are also multiples of 77, so 142 - 12 = 130. Testing with or instead of and gives a huge count, forgetting the exclusion gives 142, and testing divisibility by 77 counts only the common multiples.

More Pseudocode and Code Tracing questions

Exams that ask this topic