ProgrammingHardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift)
What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift)
Integer s = 0, x, t
for (each i from 1 to 16)
x = i
t = 0
while ((x & 1) == 0)
x = x >> 1
t = t + 1
end while
s = s + t
end for
Print s- A. 16
- B. 8
- C. 15
- D. 31
Correct answer: C. 15
t is the number of trailing zero bits of i, i.e. the power of 2 in i. Summed over 1 to 16 it is the power of 2 in 16!: 8 + 4 + 2 + 1 = 15.
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 m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Integer n = 0, c = 0 do c = c + 1 n = n + 3 while (n mod 5 != 0) Print c, n
- What will be the output of the following pseudocode? Integer cat[0..6] cat[0] = 1 for (each i from 1 to 6) cat[i] = 0 for (each j from 0…
- What will be the output of the following pseudocode? Integer a[6] = {3, 1, 2, 6, 4, 5}, swaps = 0, m, n = 6 for (each i from 0 to n - 2) m…
- What will be the output of the following pseudocode? Integer a[6] = {5, 2, 4, 6, 1, 3}, shifts = 0, key, j for (each i from 1 to 5) key =…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse; 'A'..'Z' have…