ProgrammingModerateAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer v = 0
for (i = 1 to 5)
v = (v << 1) | (i & 1)
end for
Print v- A. 31
- B. 10
- C. 21
- D. 11
Correct answer: C. 21
Each step shifts v left and appends the lowest bit of i: 1, 0, 1, 0, 1 → 10101₂ = 21. Using + instead of | gives the same here because the new bit is always free.
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 a[] = {10, 20, 30, 40} Integer i = 0, s = 0 while (i < 4) s = s + a[i] i = i…
- What will be the output of the following pseudocode? (memo starts with every entry equal to −1; calls counts every call, including the…
- What will be the output of the following pseudocode? (calls counts every call, including the first.) Integer calls = 0 Function g(Integer…
- What will be the output of the following pseudocode? (& is bitwise AND and >> is a right shift.) Integer c = 0 for (each x from 1 to 200)…
- What will be the output of the following pseudocode? (Integers are in two's complement, so −i flips the bits of i and adds 1; & is bitwise…
- What will be the output of the following pseudocode? (& is bitwise AND.) Integer c = 0, x for (each i from 0 to 63) x = i while (x > 0) x…