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- A. 2
- B. 3
- C. 10
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For i from 0 to 30 inclusive, count the values of i with ii <= 30. What is printed? count = 0 for i = 0 to 30 if i i <= 30 then count =…
- Array [4, 1, 3, 2] is sorted by selection sort: for each position i, find the minimum of the unsorted part and swap it into position i…
- Sorted array a = [1, 2, 3, 5, 8, 9] and limit L = 10. Count the pairs of distinct positions i < j with a[i] + a[j] <= L using two…
- The string s = "aaabbbcc" is cut down so that no two adjacent letters are equal, by deleting characters. The greedy keeps a character when…
- A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as…
- Count how many copies of the word BALLOON can be formed from the letters of "lloolldnaubbalo" (each letter used at most once per copy)…