ProgrammingHardProduct company OA
What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.)
What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.)
Integer x = 29, y = 30
Integer gx = x ^ (x >> 1)
Integer gy = y ^ (y >> 1)
Integer d = gx ^ gy, bits = 0
while (d > 0)
bits = bits + (d & 1)
d = d >> 1
end while
Print gx, gy, bits- A. 19 17 1
- B. 19 17 2
- C. 14 15 1
- D. 29 30 2
Correct answer: A. 19 17 1
x ^ (x >> 1) is the Gray code. 29 = 11101, 29 >> 1 = 01110 → 10011 = 19. 30 = 11110, 30 >> 1 = 01111 → 10001 = 17. 19 ^ 17 = 00010 has one set bit: consecutive numbers' Gray codes always differ in exactly one bit, while 29 and 30 themselves differ in two bits.
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; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…
- What will be the output of the following pseudocode? (calls counts every call of f, including the first.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.) Integer n = 10, c…
- What will be the output of the following pseudocode? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from…
- What will be the output of the following pseudocode? (Arrays are passed by reference and indexed from 0.) Procedure p(Integer arr[]…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0, i = 1, j while (i < 1000) j = i while (j > 0)…