ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)

What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts)
String s = "THE QUICK BROWN FOX"
Integer mask = 0, c = 0
for (each ch in s)
    if (ch != ' ')
        mask = mask | (1 << (code(ch) - code('A')))
    end if
end for
while (mask > 0)
    c = c + (mask & 1)
    mask = mask >> 1
end while
Print c
  1. A. 16
  2. B. 26
  3. C. 13
  4. D. 15

Correct answer: D. 15

Each letter sets its own bit, so the final count is the number of DIFFERENT letters: T, H, E, Q, U, I, C, K, B, R, O, W, N, F, X = 15 (O and U repeat).

More Pseudocode and Code Tracing questions

Exams that ask this topic