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 2 Question 102 Discussion

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

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

#include

#include

#include

using namespace std;

void print(int v) { cout<

struct Sequence {

int start;

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

int operator()() {

return 10*(1+(start++ %3));

}

};

int main() {

vector v1(10);

generate_n(v1.begin(), 10, Sequence(1));

remove(v1.begin(), v1.end(), 10);

for_each(v1.begin(), v1.end(), print);cout<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Delisa
1 days ago
The output is going to be 20 30 20 30 20 30 20 30 10 20. It's like a secret dance the numbers are doing, and we're just watching in awe.
upvoted 0 times
...
Julene
5 days ago
Wow, this code is really something! I bet the developer was having a fun time coming up with this one. It's like a secret code that only the true C++ masters can crack.
upvoted 0 times
...
Tasia
8 days ago
This code is going to print 20 30 10 20 30 10 20 30 10 20. The `Sequence` class generates a sequence of 10, 20, and 30, and the `remove` function removes the 10s.
upvoted 0 times
...
Emelda
10 days ago
I think the code will result in a compilation error. The `generate_n` function expects a function object, but the `Sequence` struct has a member function `operator()` instead.
upvoted 0 times
Felice
4 days ago
Yes, the `generate_n` function requires a function object, not a member function like `operator()` in the `Sequence` struct.
upvoted 0 times
...
...
Olive
14 days ago
I'm not sure, but I think it might be D) compilation error
upvoted 0 times
...
Kristeen
15 days ago
I believe the correct answer is B) 20 30 20 30 20 30 20
upvoted 0 times
...
Mozell
24 days ago
I think the answer is A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
Janessa
27 days ago
The code should compile and run without any issues, and the output should be 20 30 20 30 20 30 20.
upvoted 0 times
Jestine
4 days ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...

Save Cancel