ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Array a = [2, 4, 6, 8, 10]. The loop below sums i a[i] over the 0-based indices i. What is printed?
Array a = [2, 4, 6, 8, 10]. The loop below sums i * a[i] over the 0-based indices i. What is printed?
total = 0
for i = 0 to 4
total = total + i * a[i]
end for
Print total- A. 80
- B. 30
- C. 110
- D. 10
Correct answer: A. 80
The products are 0, 4, 12, 24 and 40, which sum to 80. Summing the values alone gives 30, using 1-based weights gives 110, and summing the indices gives 10.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The array [0, 1, 0, 3, 12] has its zeros moved to the end while keeping the order of the other values, by a two-pointer pass that writes…
- For the array a = [7, 2, 4, 9, 1, 5] the procedure below computes the sum of the minimum of every contiguous window of length 2 (windows…
- The array a = [3, 1, 2, 4] is sorted in ascending order by selection sort, and the procedure below counts the total number of element…
- Array a = [1, 2, 3, 5, 4] is checked by the procedure below, which performs bubble sort with an early-exit flag (a pass with no swaps ends…
- 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…
- 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…