ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (binary string to decimal)?

What will be the output of the following pseudocode (binary string to decimal)?
String bits = "110101"
Integer val = 0
for (each b in bits)
    val = val * 2 + digitValue(b)
end for
Print val
  1. A. 45
  2. B. 53
  3. C. 54
  4. D. 42

Correct answer: B. 53

Processing left to right, each bit doubles the accumulated value and adds the new bit: 1,3,6,13,26,53 -- matching 110101 binary = 53 decimal.

More Pseudocode and Code Tracing questions

Exams that ask this topic