ProgrammingVery hardProduct company OA
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 c = 0, i = 1, j
while (i < 1000)
j = i
while (j > 0)
j = j / 3
c = c + 1
end while
i = i * 2
end while
Print c- A. 40
- B. 10
- C. 34
- D. 45
Correct answer: C. 34
i takes 1, 2, 4, …, 512 (10 values). The inner loop counts the base-3 digits of i, i.e. ⌊log₃ i⌋ + 1: 1, 1, 2, 2, 3, 4, 4, 5, 6, 6 (e.g. 32 = 1012₃ has 4 digits, 512 = 200222₃ has 6). Sum 34. '10' counts only the outer iterations.
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.) Integer c = 0 for (n = 1 to 200) Integer s = 0, t = n while…
- What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop…
- What will be the output of the following pseudocode? (&& stops as soon as the left side is false) Integer c = 0, k = 0 Function f(Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Procedure show(Integer n) if…
- What will be the output of the following pseudocode? (| is bitwise OR) Integer x = 0, c = 0 while (x < 1000) x = x | (x + 1) c = c + 1 end…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer arr[7] = {1, 2, 3, 4, 5, 6, 7} reverse(arr, 0, 2)…