ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Array a = [3, 8, 5, 6, 7] has its odd values summed by the procedure below, and the result is printed. What is printed?
Array a = [3, 8, 5, 6, 7] has its odd values summed by the procedure below, and the result is printed. What is printed?
total = 0
for each x in a
if x mod 2 == 1 then total = total + x
end for
Print total- A. 14
- B. 15
- C. 3
- D. 29
Correct answer: B. 15
The odd values are 3, 5 and 7, summing to 15. Summing the even values gives 14, counting the odd values gives 3, and the total of the array is 29.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [0, 2, 2, 3, 5]. The procedure below counts the positions i (0-based) where a[i] equals i. What is printed? count = 0 for i = 0…
- For the array a = [1, 2, 3, 4, 5, 6], the procedure below counts the contiguous subarrays of length 3 whose sum is divisible by 3. What is…
- Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints…
- For the array a = [-1, 4, -2, 6, -3, 5], the procedure below finds the maximum sum of a contiguous window of exactly 3 elements (windows…
- Array a = [4, 9, 2, 8, 2, 7]. The procedure below counts the prefix records, that is, the positions i where a[i] is strictly greater than…
- A rotated sorted array a = [4, 5, 6, 7, 0, 1, 2] has its smallest element at some index. The procedure below finds that index by a linear…