ProgrammingModerateCapgemini · Infosys pattern
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Integer n = 4521, r = 0
while (n > 0)
r = r * 10 + n mod 10
n = n / 10
end while
Print r- A. 1245
- B. 4521
- C. 12
- D. 1254
Correct answer: D. 1254
Each pass takes the last digit (n mod 10) and appends it to r, then drops it from n. The digits come off as 1, 2, 5, 4, so r = 1254, the reverse of the number.
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? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 4) for (each j from i to 4) c = c + 1 end for end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function fun(Integer n) if (n…
- What will be the output of the following pseudocode? Integer a = 48, b = 18, t while (b != 0) t = b b = a mod b a = t end while Print a
- What will be the output of the following pseudocode? Integer p = 2, q = 3 for (each i from 1 to 3) p = p q q = q - 1 end for Print p + q