1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. std::accumulate(v.begin(), v.end(), 0) is run on a vector…

Added 30 Nov -0001

std::accumulate(v.begin(), v.end(), 0) is run on a vector of doubles. What does it return, and why?

  1. int, because the standard requires accumulate to work integrally
  2. int, because the accumulator takes the type of the initial value given Correct answer
  3. double, because it converts the seed to the range's value type
  4. double, since the accumulation widens to the element type of the range

Correct answer

B. int, because the accumulator takes the type of the initial value given

14 views

MCQSEXAM

Explanation

std::accumulate declares its accumulator with the type of the third argument, so passing the literal 0 accumulates in int and truncates every fractional part. Pass 0.0 or a value-initialised element type to accumulate in the range's own type. The trap is expecting the result type to follow the iterator's value type, as a fold expression or std::reduce would.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs