ProgrammingHardCAT · MBA placement
The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed?
The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed?
x = 2^10; total = 0 while x > 0: total = total + x mod 10; x = x div 10 Print total
- A. 4
- B. 8
- C. 7
- D. 0
Correct answer: C. 7
The digits 1, 0, 2 and 4 sum to 7. Counting the digits gives 4, the digit sum of 512 gives 8, and the product of the digits is 0 because one digit is zero.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the inversions of the array [2, 4, 1, 3] (pairs i < j with a[i] > a[j]) by checking all pairs. What is printed?…
- The procedure below counts the integers from 1 to 29 that are divisible by 3 or by 5. What is printed? count = 0 for n = 1 to 29 if n mod…
- Read the pseudocode. a = 1, b = 1, s = 0 for i = 1 to 6 t = a + b a = b b = t if (t mod 3 == 0) s = s + t else s = s - 1 end for print s…
- Consider the function. f(n): if n <= 1 return n if n mod 2 == 0 return f(n / 2) + 1 return 2 f(n - 1) + 1 What is f(13)?
- Consider the pseudocode. sum = 0 for i = 1 to 15: n = i while n != 0: n = n AND (n - 1) sum = sum + 1 print sum What is printed?
- Consider the pseudocode. s = 0 for i = 1 to 7: if i mod 3 == 0: continue j = i while j > 0: if j mod 4 == 0: break s = s + j j = j - 2…