ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a = 91, b = 35, steps = 0
While a != b
If a > b Then
a = a - b
Else
b = b - a
End If
steps = steps + 1
End While
Print a, steps- A. 7 4
- B. 7 3
- C. 7 6
- D. 7 5
Correct answer: D. 7 5
Subtractive Euclid: (91,35) -> (56,35) -> (21,35) -> (21,14) -> (7,14) -> (7,7), five subtractions, ending at gcd 7. The modulo version of Euclid would need only 4 steps, but this loop subtracts one multiple at a time.
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? Integer memo[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} Integer calls = 0…
- What will be the output of the following pseudocode? (^ is exponentiation; / is integer division truncating toward zero. An operator pops…
- What will be the output of the following pseudocode? Integer val[6] = {5, 8, 2, 9, 4, 7} Integer nxt[6] = {3, 5, 0, 1, -1, 4} Integer p =…
- What will be the output of the following pseudocode? Integer a[51] = all zeros (indices 0 to 50) For i = 1 to 50 For j = i to 50 step i…
- What will be the output of the following pseudocode? (ord('A') = 65; the letters are uppercase and the mod keeps every shift inside the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {5, 1, 4, 2, 8, 0, 3} Integer swaps = 0…