ProgrammingModerateTCS NQT · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer p[4] = {2, 0, 3, 1} // coefficients of 2x³ + 0x² + 3x + 1
Integer x = 3, m = 7, r = 0
for (each i from 0 to 3)
r = (r * x + p[i]) mod m
end for
Print r- A. 1
- B. 64
- C. 0
- D. 6
Correct answer: A. 1
Horner's rule evaluates 2x³ + 3x + 1 at x = 3 modulo 7: 54 + 9 + 1 = 64 = 9 × 7 + 1, so r = 1. Reducing at every step gives the same remainder as reducing at the end.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i i <= n; i = i + 1) for (j = 1; j <= n; j = j 2)…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[8] = {1, 2, 3, 4, 5, 6, 7, 3} Integer slow =…
- What will be the output of the following pseudocode? (mod is applied to (i j) before adding) Integer x = 0 for (each i from 1 to 5) for…