Examveda

What will be the output of the following C++ code?
#include <vector> 
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int square(int i) { return i * i; }
int main()
{
    vector<int> V, V2;
    V.push_back(0);
    V.push_back(1);
    V.push_back(2);
    transform(V.begin(), V.end(), back_inserter(V2), square);
    copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}

A. 0

B. 1

C. 2

D. 0 1 4

Answer: Option D


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it