ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (& is bitwise AND)
What will be the output of the following pseudocode? (& is bitwise AND)
Integer mask = 13, s, c = 0
s = mask
while (s > 0)
c = c + 1
s = (s - 1) & mask
end while
Print c- A. 7
- B. 13
- C. 8
- D. 3
Correct answer: A. 7
(s − 1) & mask steps through the non-empty sub-masks of mask in decreasing order. 13 = 1101 has 3 set bits, so it has 2³ − 1 = 7 non-empty sub-masks: 13, 12, 9, 8, 5, 4, 1.
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? (integers are 32-bit; & is AND, << and >> are shifts) Integer x = 45 Integer y = (x…
- What will be the output of the following pseudocode? // BST: smaller keys go left, larger (or equal) keys go right Insert into an empty…
- What will be the output of the following pseudocode? Insert into an empty BST, in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13 Queue q =…
- What will be the output of the following pseudocode? (Print writes its values on one line, separated by spaces) Integer burst[3] = {5, 3…
- What will be the output of the following pseudocode? Integer g[4][4] = {{0,0,0,0}, {0,1,0,0}, {0,0,0,1}, {1,0,0,0}} // 1 = blocked Integer…
- What will be the output of the following pseudocode? (substring(s, i, len) is len characters starting at index i; a Set keeps one copy of…