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- A. 4
- B. 7
- C. 3
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) if (b == 0) return a return gcd(b, a mod b) End…
- What will be the output of the following pseudocode? String s = "hello world of code" Integer changes = 0 for (each i from 0 to length(s)…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "ABCABCAB" Integer n = length(s), p = n for…
- What will be the output of the following pseudocode? (strings are indexed from 0; + joins strings) String s = "ABCDEF", r = "" Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0 for (each i from 1 to 5) if (a[i] > a[i…
- What will be the output of the following pseudocode? Integer a[5] = {1, 2, 3, 4, 5} Integer odd = 0, even = 1, s = 0, ans = 0 // counts of…