ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 4)
for (each j from 1 to 4)
if ((i * j) mod 3 == 0)
s = s + i
else
s = s - j
end if
end for
end for
Print s- A. -21
- B. 2
- C. -2
- D. 19
Correct answer: C. -2
i × j is a multiple of 3 only when i = 3 or j = 3. Row i = 3 adds 3 × 4 = 12, and column j = 3 for i = 1, 2, 4 adds 7, so +19. The other 9 pairs (i, j from {1, 2, 4}) subtract j: 3 × (1 + 2 + 4) = 21. 19 - 21 = -2.
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? (A do-while loop runs its body once before checking the condition; / is integer division.) Integer x = 0, c = 0…
- What will be the output of the following pseudocode? Integer p = 1 for (each i from 1 to 10) p = (p 3) mod 7 end for Print p
- The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.) Integer low = 0, high = 8…
- How many swaps does this pseudocode perform? (Arrays are indexed from 0.) Integer arr[5] = {5, 1, 4, 2, 8} Integer swaps = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n…
- What will be the output of the following pseudocode? Integer a = 3, b b = a a a = b - a b = b + a Print a mod b, b mod a