ProgrammingHardGoldman Sachs OA · Infosys SP

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Boolean seen[0..6] = all false
Integer c = 0
for (each i from 1 to 20)
    Integer r = (i * i) mod 7
    if (not seen[r])
        seen[r] = true
        c = c + 1
    end if
end for
Print c
  1. A. 4
  2. B. 7
  3. C. 3
  4. D. 6

Correct answer: A. 4

c counts the different remainders of squares mod 7. Squares of 1 to 6 give 1, 4, 2, 2, 4, 1, and 7 gives 0; the pattern repeats. So only {0, 1, 2, 4} occur: c = 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic