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 9 Question 87 Discussion

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

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

#include

#include

#include

using namespace std;

int main(){

int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };

vectorv(myints, myints+10);

set s1(v.begin(),v.end());

s1.insert(v.begin(),v.end());

s1.erase(s1.lower_bound(2),s1.upper_bound(7));

for(set::iterator i=s1.begin();i!= s1.end(); i++) {

cout<<*i<<" ";

}

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Martina
10 days ago
Hey, at least they're not using a linked list. That would be even more boring!
upvoted 0 times
...
Joaquin
12 days ago
Wait, so this code is just a bunch of people playing with containers? I thought programming was supposed to be more exciting than that!
upvoted 0 times
...
Fabiola
13 days ago
D has got to be the right answer. The program is just playing with the vector and set, so the output should be the original integers in some order.
upvoted 0 times
...
Royal
15 days ago
Hmm, I think the answer is A. The program seems to be doing some weird set operations, but I'm not sure why the output would be 0 1 8 9.
upvoted 0 times
...
Lyndia
16 days ago
I'm pretty sure the answer is C. The set will only contain unique elements, and the erase operation will remove the range [2, 7], leaving 1, 6, 5, and 7.
upvoted 0 times
...
Nickole
2 months ago
The correct answer is B. The program first creates a vector with the given integers, then creates a set from the vector, inserts the vector elements into the set, and finally erases the elements in the range [2, 7]. The resulting set contains the elements 2, 3, 4, 5, 6, 7, which are then printed.
upvoted 0 times
Naomi
29 days ago
That makes sense, the set is sorted and the elements in the range [2, 7] are erased.
upvoted 0 times
...
Naomi
1 months ago
B) program outputs: 2 3 4 5 6 7
upvoted 0 times
...
...
Haley
2 months ago
But the set will only contain unique elements, so I still think it's B.
upvoted 0 times
...
Lonny
2 months ago
I disagree, I believe the answer is C.
upvoted 0 times
...
Haley
2 months ago
I think the answer is B.
upvoted 0 times
...

Save Cancel