ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift.)
What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift.)
Integer x = 0
for (i = 0 to 7)
if (i & 1)
x = x | (1 << i)
else
x = x ^ (1 << (7 - i))
end if
end for
Print x- A. 170
- B. 255
- C. 130
- D. 160
Correct answer: D. 160
Odd i OR in bits 1, 3, 5, 7; even i TOGGLE bits 7, 5, 3, 1 – the same bits, so order matters. i0: toggle bit 7 → 128; i1: set bit 1 → 130; i2: toggle bit 5 → 162; i3: set bit 3 → 170; i4: toggle bit 3 OFF → 162; i5: set bit 5 (already on) → 162; i6: toggle bit 1 OFF → 160; i7: set bit 7 (already on) → 160. 170 is the value after i = 3; treating every step as OR gives 170 too.
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? ('ref' parameters are passed by reference; others by value.) Procedure p(ref Integer…
- What will be the output of the following pseudocode? (/ is integer division.) Function A(Integer n) if (n <= 1) return n end if return B(n…
- What will be the output of the following pseudocode? (x is an 8-bit unsigned value; & is bitwise AND, | is OR, << and >> are shifts.)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer x = 10 x = x + x 2 - 4 / 2 Print x
- What will be the output of the following pseudocode? for (i = 10; i >= 1; i = i - 3) Print i end for
- What will be the output of the following pseudocode? (/ is integer division; + on strings concatenates.) Integer n = 100 String s = ""…