ProgrammingHardAmazon OA · Goldman Sachs OA
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)
// numbers stored as digit lists, least significant digit first
List a = 2 -> 4 -> 3, b = 5 -> 6 -> 4
Integer carry = 0
List out = empty
while (a or b has digits left OR carry > 0)
Integer s = carry + (next digit of a, or 0) + (next digit of b, or 0)
append s mod 10 to out
carry = s / 10
end while
Print out- A. 8 0 7
- B. 7 10 7
- C. 8 0 7 1
- D. 7 0 8
Correct answer: D. 7 0 8
The lists hold 342 and 465 written backwards. Adding digit by digit: 2 + 5 = 7; 4 + 6 = 10 → 0 carry 1; 3 + 4 + 1 = 8. out = 7 0 8, i.e. 807 = 342 + 465 stored backwards.
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? // circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1 Node p = node 1…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What is the time complexity of the following pseudocode, as a function of n? Procedure g(Integer n) if (n <= 1) return for (each i from 1…
- What will be the output of the following pseudocode? Integer m[3][3] = {{11, 2, 4}, {4, 5, 6}, {10, 8, -12}} Integer d1 = 0, d2 = 0 for…
- What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C)) Integer x…
- What will be the output of the following pseudocode? Boolean seen[0..6] = all false Integer c = 0 for (each i from 1 to 20) Integer r = (i…