ProgrammingHardAmazon OA · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer val[7] = {1000, 500, 100, 50, 10, 5, 1}
String sym[7] = {"M", "D", "C", "L", "X", "V", "I"}
String s = "MCMXCIV"
Integer total = 0, v, nextv
for (each i from 0 to length(s) - 1)
v = value of s[i] in the table
if (i + 1 < length(s))
nextv = value of s[i + 1] in the table
else
nextv = 0
end if
if (v < nextv)
total = total - v
else
total = total + v
end if
end for
Print total- A. 2216
- B. 1994
- C. 1996
- D. 1904
Correct answer: B. 1994
A symbol smaller than the one after it is subtracted: M (+1000), C (−100), M (+1000), X (−10), C (+100), I (−1), V (+5). Total 1994. Adding everything gives 2216.
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? (code(c) gives a character's code; 'A' to 'Z' have consecutive codes) String a =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "BANANAS" Integer best = 1, l, r, n = 7 for…
- What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts; 15 = 0000 1111 and 240 = 1111 0000) Integer…
- What will be the output of the following pseudocode? Integer a[7] = {4, 1, 2, 1, 2, 5, 4} Integer x = 0 for (each i from 0 to 6) x = x ^…
- What will be the output of the following pseudocode? Integer a = 29, b = 15, x, c = 0 x = a ^ b while (x > 0) c = c + (x & 1) x = x >> 1…
- What will be the output of the following pseudocode? (integers are two's complement; ~ is bitwise NOT; >> is an arithmetic shift that…