ProgrammingHardCapgemini · TCS NQT

Array a = [4, 9, 1, 7, 3]. The procedure below finds the sum of all elements that are greater than the mean of the array. What is printed?

Array a = [4, 9, 1, 7, 3]. The procedure below finds the sum of all elements that are greater than the mean of the array. What is printed?
mean = sum / n
total = 0
for each x in a
    if x > mean then total = total + x
end for
Print total
  1. A. 8
  2. B. 20
  3. C. 16
  4. D. 2

Correct answer: C. 16

The mean is 4.8, and the elements above it are 9 and 7, summing to 16. Summing the elements below the mean gives 8, using an integer mean changes the set, and counting instead of summing gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic