ProgrammingHardInfosys SP · Cognizant GenC Next
What will be the output of the following pseudocode? (mod is applied to (i j) before adding)
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 (each j from i to 5)
x = x + (i * j) mod 3
end for
end for
Print x- A. 18
- B. 24
- C. 15
- D. 14
Correct answer: D. 14
(i × j) mod 3 is 0 whenever i or j is 3; otherwise it is 1 if i and j have the same remainder mod 3 and 2 if not. Row by row (j from i to 5): i = 1 gives 1 + 2 + 0 + 1 + 2 = 6, i = 2 gives 1 + 0 + 2 + 1 = 4, i = 3 gives 0, i = 4 gives 1 + 2 = 3, i = 5 gives 1. Total 14.
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 A[2][3] = {{1, 2, 3}, {4, 5, 6}} Integer Bm[3][2]…
- What will be the output of the following pseudocode? Function J(Integer n, Integer k) if (n == 1) return 0 end if return (J(n - 1, k) + k)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function gcd(Integer a…
- What will be the output of the following pseudocode? Function sd(Integer n) Integer s = 0 for (each d from 1 to n - 1) if (n mod d == 0) s…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) String digits…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse) String s =…