ProgrammingVery hardProduct company OA · code-tracing level
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 c = 0, x
for (each i from 0 to 63)
x = i
while (x > 0)
x = x & (x - 1)
c = c + 1
end while
end for
Print c- A. 192
- B. 384
- C. 64
- D. 160
Correct answer: A. 192
x & (x − 1) clears the lowest set bit, so the inner loop runs once per 1-bit of i. Over 0…63 every one of the 6 bit positions is set in exactly half the numbers, 32, so the total is 6 × 32 = 192. 384 assumes the inner loop runs 6 times per number; 64 counts the outer iterations only.
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 n = 30, c = 0, j for (each i from 1 to n) j = i while (j <= n) c = c + 1 j =…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1; i <= 100; i = i 2) for (j = i; j <= 100; j = j + 1) c = c +…
- What will be the output of the following pseudocode? // 32-bit signed integers; overflow wraps around (two's complement); / truncates…
- What is the time complexity of the following pseudocode, as a function of n? for (i = n; i > 0; i = i / 2) for (j = 0; j < i; j = j + 1)…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i <= n; i = i + 1) j = 1 while (j < i) j = j 2…
- What is the time complexity of the following pseudocode, as a function of n? i = 2 while (i <= n) i = i i end while