ProgrammingVery hardGoldman Sachs OA · Infosys SP
What will be the output of the following pseudocode? (^ is XOR, & is AND)
What will be the output of the following pseudocode? (^ is XOR, & is AND)
Integer s = 0, x, p
for (each i from 1 to 20)
x = i
p = 0
while (x > 0)
p = p ^ 1
x = x & (x - 1)
end while
s = s + p
end for
Print s- A. 11
- B. 9
- C. 10
- D. 20
Correct answer: C. 10
x & (x − 1) removes one set bit each time, so p ends as the parity of the number of 1-bits. s counts the numbers 1 to 20 with an odd number of 1's: 1, 2, 4, 7, 8, 11, 13, 14, 16, 19 = 10.
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? Integer P[4][4] = all 0 Integer A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each…
- 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? // BST: smaller keys go to the left, larger to the right Insert into an empty BST, in…
- What will be the output of the following pseudocode? Function ways(Integer n) if (n < 0) return 0 end if if (n == 0) return 1 end if…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; / is integer division; +…
- What will be the output of the following pseudocode? Function f(Integer n) if (n == 0) return 0 end if if (n == 1) return 1 end if return…