Cyber Monday 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
6 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
6 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
6 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
5 months ago
No, the correct answer is A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
Marion
5 months ago
C) 20 30 20 30 20 30 20 30 10 20
upvoted 0 times
...
Helga
6 months ago
C) 20 30 20 30 20 30 20 30 10 20
upvoted 0 times
...
Rutha
6 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Renea
6 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Tanja
6 months ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
Hyman
6 months ago
A) 20 30 10 20 30 10 20 30 10 20
upvoted 0 times
...
...
Hannah
7 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
6 months ago
Good explanation, thanks for clarifying!
upvoted 0 times
...
Kindra
6 months ago
Exactly, that's why the output is 20 30 20 30 20 30 20.
upvoted 0 times
...
Nicholle
6 months ago
The for_each function then prints out those remaining elements in the vector.
upvoted 0 times
...
Leigha
6 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
Nikita
6 months ago
So, the elements 10 are removed, and the remaining elements are 20 and 30.
upvoted 0 times
...
Lavonna
6 months ago
That's correct, the remove function only removes elements with the value of 10.
upvoted 0 times
...
Allene
7 months ago
B) 20 30 20 30 20 30 20
upvoted 0 times
...
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77