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
- A. 5
- B. 4
- C. 20
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [1, 1, 1, 1] (0-based). From index i you may jump forward by at most a[i] positions. The greedy below counts the minimum number…
- The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed? x = 2^10; total = 0 while x > 0…
- The procedure below counts the inversions of the array [2, 4, 1, 3] (pairs i < j with a[i] > a[j]) by checking all pairs. What is printed?…
- The procedure below counts the integers from 1 to 29 that are divisible by 3 or by 5. What is printed? count = 0 for n = 1 to 29 if n mod…
- Read the pseudocode. a = 1, b = 1, s = 0 for i = 1 to 6 t = a + b a = b b = t if (t mod 3 == 0) s = s + t else s = s - 1 end for print s…
- Consider the function. f(n): if n <= 1 return n if n mod 2 == 0 return f(n / 2) + 1 return 2 f(n - 1) + 1 What is f(13)?