ProgrammingHardTCS NQT · Accenture

The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed?

The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed?
seen = empty set
for x = 1 to 20: add (x mod 5) to seen
Print size of seen
  1. A. 5
  2. B. 4
  3. C. 20
  4. D. 6

Correct answer: A. 5

The residues mod 5 are 0, 1, 2, 3 and 4, so the set has 5 values. Stopping the range at 4 leaves 4 residues, counting the x values gives 20, and taking residues mod 6 gives 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic