ProgrammingModerateInfosys SP · TCS NQT
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 x = 255, y = 256, c1 = 0, c2 = 0
while (x > 0)
x = x & (x - 1)
c1 = c1 + 1
end while
while (y > 0)
y = y & (y - 1)
c2 = c2 + 1
end while
Print c1, c2- A. 8 9
- B. 255 256
- C. 8 1
- D. 1 8
Correct answer: C. 8 1
x & (x − 1) clears the lowest 1-bit, so each loop runs once per 1-bit: 255 = 1111 1111 has 8, and 256 = 1 0000 0000 has just 1. Output 8 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? Integer cnt = 0 Procedure go(Integer i, Integer s) if (s == 10) cnt = cnt + 1 return…
- What will be the output of the following pseudocode? Function G(Integer n) if (n == 0) return 0 end if return n - G(G(n - 1)) End Function…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code; 'A' to 'Z' have consecutive codes) String w…
- What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0) Integer a[6] = {3, 4, 5, 6, 1, 8}…
- What will be the output of the following pseudocode? (mod keeps the sign of the dividend (as in C); arrays are indexed from 0) Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer calls = 0 Procedure…