ProgrammingHardTCS Digital · Infosys InfyTQ
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 1000, c = 0
while (n != 0)
n = n & (n - 1)
c = c + 1
end while
Print c
(& is bitwise AND.)- A. 5
- B. 7
- C. 6
- D. 10
Correct answer: C. 6
Each step clears the lowest set bit, so c is the number of 1 bits in 1000 = 1111101000: 6.
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? Function g(Integer n) if (n == 0) return 0 end if if (n mod 2 == 0) return g(n - 1) +…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6} Procedure rev(Integer l, Integer r) while (l < r)…
- What will be the output of the following pseudocode? String s = "aaabccdddd", r = "" Integer i = 0, j while (i < length(s)) j = i while (j…
- What will be the output of the following pseudocode? Integer i, j, c = 0 for (i = 1; i <= 6; i = i + 1) for (j = 1; j <= 6; j = j + 1) if…
- What will be the output of the following pseudocode? Integer a = 48, b = 18, steps = 0 while (a != b) if (a > b) a = a - b else b = b - a…
- What will be the output of the following pseudocode? Integer a[15] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72, 91, 99, 101, 110, 120} Integer…