ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first)
What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first)
Integer a[5] = {2, 3, 5, 7, 8}, c = 0, s
for (each mask from 1 to 31)
s = 0
for (each i from 0 to 4)
if ((mask >> i) & 1 == 1)
s = s + a[i]
end if
end for
if (s == 10)
c = c + 1
end if
end for
Print c- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
Each mask from 1 to 31 is a non-empty subset. Subsets with sum 10: {2, 8}, {3, 7}, {2, 3, 5}. c = 3.
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 n = 6, count = 0 Integer col[n] Function ok(Integer r, Integer c) for (each k…
- What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1) Integer i = 0, c = 0 while (i++ < 5) c…
- What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value) Integer n = 5, s = 0 do s = s +…
- What will be the output of the following pseudocode? (num[i] - '0' converts a digit character to its value) String num = "79927398713"…
- What will be the output of the following pseudocode? (>> is right shift, & is bitwise AND) Integer bits[0..15], total = 0 bits[0] = 0 for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {3, 2, 2, 3, 4, 3}, k = 0, val = 3 for…