ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer x = 100, c = 0
while (x > 1)
if (x mod 2 == 0) x = x / 2
else x = 3 * x + 1
c = c + 1
end while
Print c- A. 24
- B. 26
- C. 7
- D. 25
Correct answer: D. 25
The Collatz sequence from 100: 100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 – 25 steps. Counting values instead of steps gives 26.
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 a[] = {1, 3, 5, 7, 9, 11, 13} Integer lo = 0, hi = 6, t = 11, steps = 0, mid…
- What will be the output of the following pseudocode? Integer x = 15, y = 4, r = 0 while (x >= y) x = x - y r = r + 1 end while Print r, x
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…
- What will be the output of the following pseudocode? Integer Function h(Integer n) if (n <= 0) return 1 if (n mod 2 == 1) return h(n - 1)…
- What will be the output of the following pseudocode? Integer v = 0 for (i = 1 to 5) v = (v << 1) | (i & 1) end for Print v
- What will be the output of the following pseudocode? Integer a[] = {10, 20, 30, 40} Integer i = 0, s = 0 while (i < 4) s = s + a[i] i = i…