ProgrammingHardInfosys SP · TCS Digital
What will be the output of the following pseudocode? ((b & 1) is evaluated before == ; & is AND, << and >> are shifts)
What will be the output of the following pseudocode? ((b & 1) is evaluated before == ; & is AND, << and >> are shifts)
Integer a = 13, b = 11, p = 0, adds = 0
while (b > 0)
if (b & 1 == 1)
p = p + a
adds = adds + 1
end if
a = a << 1
b = b >> 1
end while
Print p, adds- A. 143 4
- B. 143 11
- C. 130 3
- D. 143 3
Correct answer: D. 143 3
Shift-and-add multiplication: 11 = 1011 in binary, so p = 13 + 26 + 104 = 143, using one addition per 1-bit of b: 3. Output 143 3.
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 right shift) Integer s = 0, g for (each i from 0 to 7) g = i ^ (i >>…
- What will be the output of the following pseudocode? Function possible(Integer out[5]) Stack st = empty Integer next = 1 for (each i from…
- What will be the output of the following pseudocode? Integer coins[4] = {9, 6, 5, 1} // sorted largest first Integer amt = 11, used = 0…
- What will be the output of the following pseudocode? Integer a[8] = {10, 9, 2, 5, 3, 7, 101, 18} Integer L[8], best = 0 for (each i from 0…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "SUNDAY", y = "SATURDAY" Integer…
- What will be the output of the following pseudocode? Integer a[6] = {5, 5, 10, 100, 10, 5} Integer incl = 0, excl = 0, t for (each i from…