ProgrammingModerateInfosys · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer x = 1
for (k = 1 to 10)
x = (x * 3) mod 7
end for
Print x- A. 1
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Powers of 3 mod 7 cycle with period 6: 3, 2, 6, 4, 5, 1, 3, 2, 6, 4. After 10 steps x = 3¹⁰ mod 7 = 3⁴ mod 7 = 4. '1' assumes the cycle has just completed (that happens at k = 6 and 12).
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? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (i = 1 to 4) arr[i]…
- What will be the output of the following pseudocode? (calls counts every call, including the first; mod is the remainder.) Integer calls =…
- What will be the output of the following pseudocode? (& is bitwise AND.) Integer total = 0 for (i = 1 to 15) Integer n = i while (n != 0)…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 0; i < 10; i = i + 1) if (i mod 3 == 0) i = i + 2 end if c…
- What will be the output of the following pseudocode? (&& binds more tightly than ||; ! is NOT.) Integer a = 5, b = 10, c = 15 if (a > b ||…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 6, steps = 0, peak = n while (n != 1) if (n mod…