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:

Giuseppe
1 months ago
This question is a real brainteaser! I bet the compiler will throw an error because the Sequence struct is not copyable. That would be hilarious! Anyway, I'm going with E) compilation error.
upvoted 0 times
Gwenn
7 days ago
Let's see what happens when we actually compile and run the code.
upvoted 0 times
...
Gwenn
20 days ago
I agree, I also think there might be a compilation error.
upvoted 0 times
...
Gwenn
28 days ago
I believe there might be a compilation error due to the Sequence struct not being copyable.
upvoted 0 times
...
Gwenn
1 months ago
I think the code will compile without any errors.
upvoted 0 times
...
...
Lorriane
1 months ago
Hmm, I'm not sure about this one. Let me think it through... Ah, I got it! The correct answer must be D) 11 10 9 8 7 6 5 4 3 2. The Sequence struct generates a sequence of 10 numbers, but the sort function reverses the order, and the unique_copy() function removes the duplicates.
upvoted 0 times
...
Tori
2 months ago
I'm pretty sure the answer is A) 1 2 3 4 5 6 7 8 9 10. The Sequence struct generates a sequence of 10 numbers, and the unique_copy() function simply copies the unique elements to the second vector.
upvoted 0 times
Celestina
26 days ago
Yes, the code generates a sequence and then copies the unique elements to the second vector, resulting in A) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
Celestina
28 days ago
I think you're correct, it should be A) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
...
Izetta
2 months ago
But the code sorts the vector in descending order, so the answer should be C.
upvoted 0 times
...
Paris
2 months ago
I disagree, I believe the answer is B.
upvoted 0 times
...
Izetta
2 months ago
I think the answer is A.
upvoted 0 times
...
Arletta
2 months 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
Lelia
4 hours ago
That makes sense. The unique_copy function then copies the unique elements to the second vector.
upvoted 0 times
...
Lelia
16 days ago
Oh, I see. The sort function only reverses the order, it doesn't change the values.
upvoted 0 times
...
Lelia
1 months ago
No, the correct output is A) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
Lelia
1 months ago
I think the output will be B) 2 3 4 5 6 7 8 9 10 11.
upvoted 0 times
...
...
Clay
2 months 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