ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including the empty set. It prints how many of these subsets have an even sum. What is printed?
The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including the empty set. It prints how many of these subsets have an even sum. What is printed?
count = 0
for mask = 0 to 7
s = sum of (i+1) for each bit i set in mask
if s mod 2 == 0 then count = count + 1
end for
Print count- A. 5
- B. 3
- C. 2
- D. 4
Correct answer: D. 4
The sums of the eight subsets are 0, 1, 2, 3, 3, 4, 5 and 6, of which 0, 2, 4 and 6 are even, giving 4. Counting sums of at least 3 gives 5, leaving out the empty set gives 3, and counting sums divisible by 4 gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The number of trailing zeros in the decimal form of 2^20 5^3 is found by the procedure below, which divides out factors of 10 one at a…
- The procedure below computes the integer square root of 200 by testing r = 0, 1, 2, ... and stopping at the largest r with (r + 1) (r + 1)…
- The procedure below finds the smallest 0-based index i at which the prefix sum of [4, -1, 3, -2, 5] reaches at least 7. It prints i. What…
- Two sorted arrays a = [1, 4, 7] and b = [2, 3, 8, 9] are merged by the two-pointer procedure below, and the procedure prints the 4th…
- The procedure below repeatedly replaces x by the sum of its decimal digits, starting from x = 2^15, until x is a single digit, and prints…
- An array holds the values [3, 0, 2, 0, 1] (positions 0 to 4). The procedure below computes the water trapped above each position as…