ProgrammingHardAccenture · Cognizant GenC Next

What will be the output of the following pseudocode? (Strings are indexed from 0)

What will be the output of the following pseudocode? (Strings are indexed from 0)
String s = "110100"
Integer v = 0, p = 1
for (each i from 0 to length(s) - 1)
    if (s[i] == '1')
        v = v + p
    end if
    p = p * 2
end for
Print v
  1. A. 52
  2. B. 13
  3. C. 11
  4. D. 44

Correct answer: C. 11

The weight p doubles while moving left to right, so the string is read with its first character as the units bit: 1 + 2 + 8 = 11. Reading it as ordinary binary gives 52.

More Pseudocode and Code Tracing questions

Exams that ask this topic