BlackFriday 2024! 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 93 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 93
Topic #: 1
[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:

Carin
28 days ago
Wait, is this a trick question? The real answer is to scrap the whole thing and use Python instead. Much less headache.
upvoted 0 times
Bettina
12 days ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...
Magda
29 days ago
Hmm, let's see... Is the answer 42? Because that's the answer to everything, right?
upvoted 0 times
...
Jonell
1 months ago
C looks right to me. I mean, what could go wrong with that convoluted `Sequence` struct? It's probably just an elegant and concise way to generate the numbers.
upvoted 0 times
Barbra
4 days ago
C) 20 30 20 30 20 30 20 30 10 20
upvoted 0 times
...
Nina
9 days ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Ronnie
15 days ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...
Dominic
1 months ago
I'm going with A. Who doesn't love a good old-fashioned off-by-one error? Classic!
upvoted 0 times
...
Rosendo
1 months ago
D, definitely a compilation error! Why would they use `remove()` on a vector when they should be using `erase()`? Rookie mistake.
upvoted 0 times
Theola
7 days ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...
Carolynn
1 months ago
The correct answer is B. The code generates a sequence of 10 numbers starting from 1 and incrementing by 3, then removes the value 10 from the vector before printing the remaining numbers.
upvoted 0 times
Ellsworth
16 days ago
I see, so the correct answer is B because the code generates a sequence and removes the value 10 from the vector.
upvoted 0 times
...
Richelle
21 days ago
That makes sense, the code removes the value 10 before printing the remaining numbers.
upvoted 0 times
...
Merilyn
1 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
...
Barb
2 months ago
I agree with Junita, D) compilation error makes sense since remove function is not used correctly
upvoted 0 times
...
Junita
2 months ago
I believe it's D) compilation error because there seems to be an issue with the remove function
upvoted 0 times
...
Dorothy
2 months ago
I think the answer is A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...

Save Cancel