ProgrammingModerateAccenture · Cognizant pattern
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.)
Integer n = 100, c = 0
while (n > 1)
if (n mod 2 == 0)
n = n / 2
else
n = 3 * n + 1
end if
c = c + 1
end while
Print c- A. 17
- B. 25
- C. 26
- D. 24
Correct answer: B. 25
This is the Collatz sequence: 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. That is 25 steps, and c counts steps, not the 26 values.
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? (All variables are integers, so / is integer division.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (each i from 1 to…
- What will be the output of the following pseudocode? Integer x = 0 for (each i from 1 to 100) if (i mod 4 == 0) continue end if if (i >…
- What will be the output of the following pseudocode? Integer a = 10, b = 3, c = 0 while (a >= b) a = a - b c = c + 1 end while Print c, a
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 5) for (each j from 1 to 5) if (j > i) break end…