ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative integers.)
What will be the output of the following pseudocode? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative integers.)
Integer n = 156
Integer g = n ^ (n >> 2)
Integer h = g
Integer r = 0
While g > 0
r = r + (g & 1)
g = g >> 1
End While
Print h, r- A. 210 4
- B. 143 5
- C. 187 6
- D. 4 1
Correct answer: C. 187 6
156 = 10011100 in binary and 156 >> 2 = 00100111 (39); XOR gives 187 (10111011), which has 6 one-bits. The loop only counts set bits, and h keeps the value of g from before it is shifted away.
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? (break leaves only the innermost loop; continue skips to the next iteration of the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[6] = {4, 7, 1, 9, 3, 6} Integer i = 0 For k = 1 to…
- What will be the output of the following pseudocode? String s = "ABCDEF", r = "" For i = 0 to 5 If i mod 2 = 0 Then r = s[i] + r Else r =…
- What will be the output of the following pseudocode? Integer a = 91, b = 35, steps = 0 While a != b If a > b Then a = a - b Else b = b - a…
- What will be the output of the following pseudocode? Integer memo[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} Integer calls = 0…
- What will be the output of the following pseudocode? (^ is exponentiation; / is integer division truncating toward zero. An operator pops…