ProgrammingHardCapgemini · Infosys 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 n = 45, c = 0
while (n > 0)
n = n & (n - 1)
c = c + 1
end while
Print c- A. 6
- B. 4
- C. 3
- D. 5
Correct answer: B. 4
n & (n - 1) clears the lowest set bit, so the loop runs once per 1-bit. 45 = 101101₂ has four 1s.
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 x = 0 for (each i from 1 to 5) for (each j from 1 to i) if ((i + j) mod 2 ==…
- What will be the output of the following pseudocode? Function fun(Integer n) if (n > 0) fun(n - 1) Print n fun(n - 1) end if End Function…
- What will be the output of the following pseudocode? Integer a = 1, b = 1, c for (each i from 1 to 6) c = a + b a = b b = c end for Print b
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0 while…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (each i from 1 to…