ProgrammingHardInfosys SP · TCS Digital
What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
Integer x = 7, m = 1
while ((x & m) != 0)
x = x ^ m
m = m << 1
end while
x = x ^ m
Integer y = 12
m = 1
while ((y & m) != 0)
y = y ^ m
m = m << 1
end while
y = y ^ m
Print x, y- A. 15 13
- B. 8 13
- C. 6 11
- D. 8 12
Correct answer: B. 8 13
This adds 1 using only bit operations: it clears the trailing 1's and sets the next 0. 7 = 0111 → 1000 = 8; 12 = 1100 has no trailing 1's, so only the last bit is set: 13. Output 8 13.
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 printed = 0 Procedure perm(String s, Integer l) // s is passed by value if (l…
- What will be the output of the following pseudocode? Integer t[4][4] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}} // row r has r + 1 numbers…
- What will be the output of the following pseudocode? Integer a = 20, b = 6, c = 0 while (a > b) a = a - 3 b = b + 2 c = c + 1 end while…
- What will be the output of the following pseudocode? Integer r[3][4] = {{1, 2, 3, 4}, {2, 4, 6, 8}, {4, 2, 5, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? // list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL Node fast = head, slow = head Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {10, 5, 3, 4, 3, 5, 6} Set seen = empty Integer first = -1 for (each i…