ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
Integer a = 29, b = 0
while (a > 0)
b = (b << 1) | (a & 1)
a = a >> 1
end while
Print b- A. 23
- B. 15
- C. 27
- D. 29
Correct answer: A. 23
Each pass moves the lowest bit of a onto the end of b, so b gets a's bits in reverse. 29 = 11101 reversed is 10111 = 16 + 4 + 2 + 1 = 23.
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? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- How many numbers are printed when fun(5) is called? Function fun(Integer n) if (n <= 0) return end if fun(n - 1) Print n fun(n - 2) End…
- What will be the output? (A static variable keeps its value between calls and is initialised only once. The two calls in Print are…
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = b + c b = a - b c c = a b - c Print a + b + c
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer s = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0, d = 5…