ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign Integer x = 7, y = 3 Print (x / y) * y + (x mod y), (-x) / y, (-x) mod y
- A. 7 -3 2
- B. 6 -2 -1
- C. 7 -2 2
- D. 7 -2 -1
Correct answer: D. 7 -2 -1
(x / y)·y + x mod y always rebuilds x: 2·3 + 1 = 7. With truncation, −7 / 3 = −2 and −7 mod 3 = −1 (so that −2·3 + (−1) = −7). Floor-based languages such as Python give −3 and 2 instead.
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? Integer Function g(Integer n) if (n <= 1) return n return g(n - 1) + 2 g(n - 2) End…
- What will be the output of the following pseudocode? String s = "" Integer n = 13 while (n > 0) s = (n mod 2) + s // prepend the digit n =…
- What will be the output of the following pseudocode? Integer i = 1, j, c = 0 while (i <= 16) j = i while (j <= 16) c = c + 1 j = j 2 end…
- What will be the output of the following pseudocode? Integer a = 0 for (i = 1 to 10) if (i mod 2 == 0) continue if (i > 7) break a = a + i…
- What will be the output of the following pseudocode? Integer n = 6, f[0..6], s = 0 f[0] = 0, f[1] = 1 for (i = 2 to n) f[i] = f[i - 1] +…
- What will be the output of the following pseudocode? Integer x = 182 // binary 1011 0110, 8-bit register Print x >> 2, (x << 1) & 255, x &…