ProgrammingHardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)
What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)
Integer c = 0, r, x
for (each n from 1 to 100)
r = 0
x = n
while (x > 0)
r = (r << 1) | (x & 1)
x = x >> 1
end while
if (r == n)
c = c + 1
end if
end for
Print c- A. 16
- B. 10
- C. 20
- D. 19
Correct answer: D. 19
r is n with its binary digits reversed, so c counts binary palindromes up to 100. By length: 1 (1), 1 (11), 2 (101, 111), 2, 4, 4, and with 7 bits only those ≤ 100 (= 1100100): 1000001, 1001001, 1010101, 1011101, 1100011 (99) → 5. Total 1 + 1 + 2 + 2 + 4 + 4 + 5 = 19.
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 iso(String a, String b) Map f = empty, g = empty for (each i from 0 to…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "AEBCBDA" String t = reverse of s Integer n =…
- What will be the output of the following pseudocode? Integer p[6] = {7, 1, 5, 3, 6, 4} Integer lo = p[0], one = 0, many = 0 for (each i…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {2, 3, 1, 1, 4} Integer jumps = 0, curEnd…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer h[9] = {1, 8, 6, 2, 5, 4, 8, 3, 7} Integer i = 0…
- What will be the output of the following pseudocode? (>> is right shift, & is AND) Integer a[3] = {4, 14, 2}, total = 0 for (each b from 0…