ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a = 100, b = 7, steps = 0
while (a != b)
if (a > b)
a = a - b
else
b = b - a
end if
steps = steps + 1
end while
Print a, steps- A. 1 14
- B. 2 14
- C. 1 18
- D. 1 17
Correct answer: C. 1 18
Subtraction-based Euclid: the gcd stays the same at every step. 100 − 14 × 7 = 2, so the first 14 steps bring a down to 2 (b = 7). Then b: 7 → 5 → 3 → 1 (3 steps). Then a: 2 → 1 (1 step), and a = b = 1. Steps = 14 + 3 + 1 = 18, gcd = 1. '1 14' counts only the first phase; '1 17' forgets the final step that makes a equal to b.
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? (/ is integer division; calls counts every call, including the first.) Integer calls…
- What will be the output of the following pseudocode? (^ is bitwise XOR and >> is a right shift.) Integer g = 59, n = 0 while (g > 0) n = n…
- How many times does the body of this loop run? // c is an 8-bit unsigned integer: it holds 0 to 255 and arithmetic wraps modulo 256…
- What will be the output of the following pseudocode? (/ is integer division.) Integer a = 7, b = 3 a = a b - a / b Print a
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 20) if (i mod 3 == 0 or i mod 5 == 0) c = c + 1 end if…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 2405, c = 0, s = 0 while (n > 0) s = s + n mod…