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

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

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

#include

#include

using namespace std;

int main() {

int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };

string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"};

map m;

for (int i = 0; i < 10; i++) {

m.push_back(pair(t[i], s[i]));

}

for (map::iterator i = m.begin(); i != m.end(); i++) {

cout << i?>first << " ";

}

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Sheron
2 days ago
I'm going with option E. The map will preserve the order of the elements, so it'll print out all the values.
upvoted 0 times
...
Dana
5 days ago
Option D seems like the way to go. The map should store the unique keys and their corresponding values.
upvoted 0 times
...
Carissa
8 days ago
Haha, option C would be hilarious! The map just prints out the keys without caring about the values. What a mess!
upvoted 0 times
...
Loren
13 days ago
I believe the error is due to the use of push_back with a map.
upvoted 0 times
...
Annett
19 days ago
I agree with Brock, there seems to be an issue with the code.
upvoted 0 times
...
Providencia
1 months ago
I think the correct answer is B. The code has a compilation error because you can't use push_back() with a map.
upvoted 0 times
...
Octavio
1 months ago
The output should be option A, since the map will automatically handle the duplicate keys and only store unique values.
upvoted 0 times
Scot
4 days ago
B) compilation error
upvoted 0 times
...
Scot
6 days ago
B) compilation error
upvoted 0 times
...
...
Brock
1 months ago
I think the answer is B) compilation error.
upvoted 0 times
...

Save Cancel