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- A. 16
- B. 26
- C. 13
- 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).
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? (Arrays are indexed from 0; abs gives the absolute value) Integer a[8] = {4, 3, 2, 7…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[4] = {3, 4, -1, 1}, n = 4 for (each i from 0 to…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[3] = {1, 3, 8}, b[4] = {2, 7, 9, 10}, c[7]…
- What will be the output of the following pseudocode? Integer m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} for (each i from 0 to…
- What will be the output of the following pseudocode? (/ is integer division) String tok[9] = {"-", "", "+", "2", "3", "4", "/", "8", "2"}…
- What will be the output of the following pseudocode? Stack s = empty, mins = empty Procedure push2(Integer x) push(s, x) if (mins is empty…