ProgrammingHardTCS NQT · Accenture
The procedure below converts the Roman numeral "MCMXCIV" to a number by reading left to right and subtracting a symbol's value when the next symbol is larger, otherwise adding it. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. What is printed?
The procedure below converts the Roman numeral "MCMXCIV" to a number by reading left to right and subtracting a symbol's value when the next symbol is larger, otherwise adding it. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. What is printed?
total = 0
for i = 0 to n - 1
if i + 1 < n and value(s[i]) < value(s[i+1]) then total = total - value(s[i])
else total = total + value(s[i])
end for
Print total- A. 1994
- B. 2216
- C. -1984
- D. 1989
Correct answer: A. 1994
MCMXCIV is 1000 + 900 + 90 + 4 = 1994. Adding every symbol gives 2216, reversing the comparison gives -1984, and skipping the last symbol changes the total to 1989.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A recursive function is written as below. The procedure counts the calls made for input 7, including the first call. What is printed?…
- The procedure below uses exponentiation by squaring on the exponent 37. It counts one multiplication into result each time the current…
- Recursive merge sort splits an array of length n into halves of length n div 2 and n minus n div 2, and calls itself on each half unless…
- The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed? i =…
- A nested loop counts the pairs (i, j) with 1 <= j <= i <= 6 that also satisfy i + j even. The procedure prints the count. What is printed?…
- The procedure below counts the numbers i from 1 to 100 whose binary form has exactly three 1 bits (bits are counted by repeatedly taking n…