ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Items of weights 7, 3, 5, 2, 6 are considered in increasing weight order. Each item is taken if it still fits in the capacity 13. What is printed as the total weight taken?

Items of weights 7, 3, 5, 2, 6 are considered in increasing weight order. Each item is taken if it still fits in the capacity 13. What is printed as the total weight taken?
sort weights ascending; cap = 13; total = 0
for each weight w in order
    if w <= cap then cap = cap - w; total = total + w
end for
Print total
  1. A. 2
  2. B. 3
  3. C. 10
  4. D. 5

Correct answer: C. 10

Taking 2, 3 and 5 uses 10, and then 6 does not fit in the remaining 3. The total weight taken is 10; counting items gives 3, taking the heaviest first counts 2 items, and counting all items ignores the capacity.

More Pseudocode and Code Tracing questions

Exams that ask this topic