ProgrammingVery hardTCS Digital · Capgemini
What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C))
What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C))
Integer x = -123, r = 0
while (x != 0)
r = r * 10 + x mod 10
x = x / 10
end while
Print r- A. -321
- B. 123
- C. -3
- D. -312
Correct answer: A. -321
With C-style arithmetic, −123 mod 10 = −3 and −123 / 10 = −12, so the digits come out negative: r = −3, −32, −321. The sign is kept automatically: −321.
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? Boolean seen[0..6] = all false Integer c = 0 for (each i from 1 to 20) Integer r = (i…
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) if (b == 0) return a return gcd(b, a mod b) End…
- What will be the output of the following pseudocode? String s = "hello world of code" Integer changes = 0 for (each i from 0 to length(s)…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "ABCABCAB" Integer n = length(s), p = n for…
- What will be the output of the following pseudocode? (strings are indexed from 0; + joins strings) String s = "ABCDEF", r = "" Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0 for (each i from 1 to 5) if (a[i] > a[i…