Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 1 Question 61 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 61
Topic #: 1
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator()(const T & val ) {

out<

}

};

struct Sequence {

int start;

Sequence(int start):start(start){}

int operator()() { return 10*(1+(start++ %3)); } };

int main() {

vector v1(10);

vector v2(10);

generate(v1.begin(), v1.end(), Sequence(1));

sort(v1.rbegin(), v1.rend());

unique_copy(v1.begin(),v1.end(), v2.begin());

for_each(v2.begin(), v2.end(), Out(cout) );cout<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Izetta
3 days ago
But the code sorts the vector in descending order, so the answer should be C.
upvoted 0 times
...
Paris
5 days ago
I disagree, I believe the answer is B.
upvoted 0 times
...
Izetta
6 days ago
I think the answer is A.
upvoted 0 times
...
Arletta
6 days ago
I think the output will be B) 2 3 4 5 6 7 8 9 10 11. The Sequence struct generates numbers in the range of 10 to 30, and the sort function reverses the order.
upvoted 0 times
...
Clay
8 days ago
The output should be C) 10 9 8 7 6 5 4 3 2 1. The code generates a sequence of 10 numbers starting from 1 and incrementing by 3, then sorts the vector in reverse order, and finally removes the duplicates using unique_copy().
upvoted 0 times
...

Save Cancel