ProgrammingHardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift)
What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift)
Integer x = 11 Integer a = (~x) & (x + 1) Integer y = 37 y = y | (y >> 1) y = y | (y >> 2) y = y | (y >> 4) Print a, y
- A. 1 37
- B. 4 63
- C. 4 64
- D. 8 63
Correct answer: B. 4 63
(~x) & (x + 1) isolates the lowest 0-bit of x: 11 = 1011, so it is 0100 = 4. OR-ing y with its shifts copies the top 1-bit into every lower position: 37 = 100101 becomes 111111 = 63. Output 4 63.
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 XOR, & is AND) Integer a = 10, b = 20, x, flips = 0 x = a ^ b while (x > 0) x =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function f(Integer n) if (n <=…
- What will be the output of the following pseudocode? Integer calls = 0 Procedure f(Integer n) calls = calls + 1 for (each i from 1 to n…
- What will be the output of the following pseudocode? Integer t[0..10] t[0] = 0 t[1] = 1 t[2] = 1 for (each i from 3 to 10) t[i] = t[i - 1]…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "{[()]}([)]" Stack st = empty Integer bad =…
- What will be the output of the following pseudocode? Queue q = {1, 2, 3, 4, 5} Integer k = 3 Stack st = empty for (each i from 1 to k)…