ProgrammingHardWipro · Capgemini pattern
What will be the output of the following pseudocode? (/ is integer division.)
What will be the output of the following pseudocode? (/ is integer division.)
Integer n = 6, steps = 0, peak = n
while (n != 1)
if (n mod 2 == 0)
n = n / 2
else
n = 3 * n + 1
end if
if (n > peak)
peak = n
end if
steps = steps + 1
end while
Print steps, peak- A. 9 16
- B. 8 16
- C. 8 10
- D. 7 16
Correct answer: B. 8 16
6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1: eight steps, and the largest value reached is 16. '9' counts the starting value as a step; '10' stops tracking the peak too early.
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? (reverse(x) returns x with its decimal digits reversed.) Integer n = 87, k = 0 while…
- What will be the output of the following pseudocode? Procedure fun(Integer n) if (n <= 0) return end if fun(n - 2) Print n fun(n - 1) End…
- What will be the output of the following pseudocode? (/ is integer division.) Integer a = 1, b = 100, steps = 0 while (a < b) a = a 2 b =…
- What will be the output of the following pseudocode? (calls counts every call, including the first; memo starts with every entry equal to…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0 for (n = 1 to 50) Integer d = 0 for (k = 1 to…
- What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift.) Integer x = 0 for (i = 0 to…