ProgrammingHardProduct company OA · Infosys pattern
What will be the output of the following pseudocode? (& is bitwise AND.)
What will be the output of the following pseudocode? (& is bitwise AND.)
Integer total = 0
for (i = 1 to 15)
Integer n = i
while (n != 0)
n = n & (n - 1)
total = total + 1
end while
end for
Print total- A. 15
- B. 30
- C. 32
- D. 64
Correct answer: C. 32
n & (n − 1) clears the lowest set bit, so the inner loop runs once per 1-bit. Across 0..15 (all 4-bit patterns) each bit position is 1 in half of the 16 numbers: 4 × 8 = 32 one-bits, and 0 contributes nothing. '15' counts numbers, not bits.
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 c = 0, i for (i = 0; i < 10; i = i + 1) if (i mod 3 == 0) i = i + 2 end if c…
- What will be the output of the following pseudocode? (&& binds more tightly than ||; ! is NOT.) Integer a = 5, b = 10, c = 15 if (a > b ||…
- 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…
- 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 =…