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 85 Discussion

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

Ming
5 months ago
Hmm, I'm going with option A. The `generate_n` function creates the sequence, and the `remove` function doesn't remove the last set of 10s, so the output should be the full sequence.
upvoted 0 times
...
Emiko
5 months ago
LOL, I bet the answer is D. The code has a bunch of includes and `using namespace std;`, so it's probably a compilation error. What a classic trap question!
upvoted 0 times
...
Virgie
5 months ago
I think option C is correct. The `generate_n` function fills the vector with the sequence of 20, 30, 10, and the `remove` function removes the 10s, leaving the final output.
upvoted 0 times
Helga
4 months ago
No, the correct answer is A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
Marion
4 months ago
C) 20 30 20 30 20 30 20 30 10 20
upvoted 0 times
...
Helga
5 months ago
C) 20 30 20 30 20 30 20 30 10 20
upvoted 0 times
...
Rutha
5 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Renea
5 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Tanja
5 months ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
Hyman
5 months ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...
Hannah
6 months ago
The program should output option B, as the `remove` function only removes the elements with the value of 10, leaving the remaining elements in the vector.
upvoted 0 times
Lauran
5 months ago
Good explanation, thanks for clarifying!
upvoted 0 times
...
Kindra
5 months ago
Exactly, that's why the output is 20 30 20 30 20 30 20.
upvoted 0 times
...
Nicholle
5 months ago
The for_each function then prints out those remaining elements in the vector.
upvoted 0 times
...
Leigha
5 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Nikita
5 months ago
So, the elements 10 are removed, and the remaining elements are 20 and 30.
upvoted 0 times
...
Lavonna
5 months ago
That's correct, the remove function only removes elements with the value of 10.
upvoted 0 times
...
Allene
6 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
...

Save Cancel