ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, << and >> are shifts.)
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, << and >> are shifts.)
Integer x = 22, r = 0
for (each i from 1 to 8)
r = (r << 1) | (x & 1)
x = x >> 1
end for
Print r- A. 13
- B. 26
- C. 208
- D. 104
Correct answer: D. 104
The loop takes bits of x from the right and pushes them into r from the right, so r ends up as x's 8 bits in reverse order. 22 = 00010110, reversed 01101000 = 64 + 32 + 8 = 104. 13 = 1101 reverses only the significant bits 10110 → 01101, forgetting that the loop runs 8 times and the leading zeros become trailing zeros.
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? (Arrays are indexed from 0; / is integer division.) Integer a[7] = {1, 3, 5, 5, 5, 7…
- What will be the output of the following pseudocode? Integer s = 0 Procedure p(Integer n) if (n == 0) return end if p(n - 1) s = s + n p(n…
- What will be the output of the following pseudocode? (/ is integer division.) Function A(Integer n) if (n <= 0) return 1 end if return B(n…
- What will be the output of the following pseudocode? (g is a global variable.) Integer g = 0 Function f(Integer n) if (n == 0) return 0…
- What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the…
- What will be the output of the following pseudocode? (&& binds more tightly than ||, and both stop evaluating as soon as the result is…