ProgrammingHardTCS NQT · Accenture
The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed?
The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed?
rev = 0; m = N
while m > 0
rev = rev * 10 + (m mod 10)
m = m div 10
end while
Print rev- A. 120450
- B. 120
- C. 540210
- D. 54021
Correct answer: D. 54021
Reversing the digits gives 054021, which as a number is 54021, so the trailing zero of the original becomes a leading zero that disappears. Printing the number unchanged, using the wrong place value, and shifting the result by a factor of ten each give different values.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- An Armstrong number of three digits equals the sum of the cubes of its digits. The procedure below counts how many three-digit numbers…
- Compute C(10, 3) modulo 7 using Pascal's rule C(n, k) = C(n-1, k-1) + C(n-1, k), reducing every entry mod 7. What is printed? build the…
- By Fermat's little theorem, for a prime p and a not divisible by p, the inverse of a is a^(p-2) mod p. The procedure below finds the…
- A pair (a, b) with 1 <= a < b <= 20 is called coprime if gcd(a, b) = 1. The procedure below counts coprime pairs. What is printed? count =…
- The sum of the divisors of N = 28 is computed by the procedure below. A number is perfect when its proper divisors (excluding N) add up to…
- The procedure below counts the digits of 2^10 by repeated division by 10. What is printed? N = 2^10; d = 0 while N > 0 N = N div 10; d = d…