ProgrammingModerateAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 45, c = 0
while (n != 0)
c = c + (n & 1)
n = n >> 1
end while
Print c- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
45 = 101101 in binary. The loop adds the lowest bit and shifts right, counting the 1 bits: 4. It runs once per bit (6 times).
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 x = 10, s = 0 while (x > 0) s = s + x x = x - 3 end while Print s, x
- What will be the output of the following pseudocode? Integer a = 10, b = 20, c = 30, r if (a > b AND b > c) r = 1 else if (a < b OR c < b)…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 1) return 1 return f(n / 2) + n // integer…
- What will be the output of the following pseudocode? Integer count = 0 for (i = 2 to 30) Boolean prime = true for (d = 2 while d d <= i)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6 for (i = 0 to n/2 - 1) swap(a[i], a[n - 1…
- What will be the output of the following pseudocode? // C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign…