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 CPP Exam - Topic 9 Question 110 Discussion

Posted by Solange on Jul 04, 2025 at 11:52 PM

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;

}

Suggested Answer: B

Get Premium CPP Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
26 comments · newest first
NA
Nathalie 10 months ago
Wait, are you sure? I thought it would just ignore duplicates.
RO
Ronna 10 months ago
Totally agree, maps use insert, not push_back!
HA
Haydee 10 months ago
Looks like a compilation error due to push_back on map.
GR
Gracie 10 months ago
I thought it would output 1 2 3 4 5, but now I'm confused!
DA
Daniela 11 months ago
Definitely a compilation error, no doubt about it.
CO
Cordelia 11 months ago
I remember that the `map` should store unique keys, so I guess it will output "one two three four five" based on the last occurrences.
BE
Bea 11 months ago
I practiced a similar question, and I think the iterator part might have a syntax issue with `i?>first`.
MA
Mattie 11 months ago
I’m not sure, but I feel like the output should be unique keys, so maybe it will print "1 2 3 4 5"?
VE
Veronika 11 months ago
I think there’s a compilation error because I remember that `map` doesn’t have a `push_back` method.
AM
Amos 12 months ago
Ah, I see the problem now. The map is going to store each key-value pair, but since the keys are duplicates, it will only output the unique keys.
VI
Vinnie 12 months ago
I'm a bit confused about how the map is being populated. Shouldn't we be using the map.insert() method instead of push_back()?
VI
Virgina 12 months ago
I'm pretty confident I know the answer to this one. The map will automatically sort the keys, so the output should be the unique numbers in order.
DE
Deonna 1 year ago
Okay, I think I see the issue. The map.push_back() method doesn't exist, so that's going to cause a compilation error.
FE
Felicitas 1 year ago
Hmm, this looks tricky. I'll need to carefully read through the code and think about how the map data structure works.
SH
Sheron 1 year ago
I'm going with option E. The map will preserve the order of the elements, so it'll print out all the values.
EV
Evan 1 year ago
↳ replying to Sheron
Yes, I agree. The program will output: one one two two three three four four five five.
PR
Princess 1 year ago
↳ replying to Sheron
I think it's option E because the map will keep the order of the elements.
DA
Dana 1 year ago
Option D seems like the way to go. The map should store the unique keys and their corresponding values.
CA
Carissa 1 year ago
Haha, option C would be hilarious! The map just prints out the keys without caring about the values. What a mess!
LO
Loren 1 year ago
I believe the error is due to the use of push_back with a map.
AN
Annett 1 year ago
I agree with Brock, there seems to be an issue with the code.
PR
Providencia 1 year ago
I think the correct answer is B. The code has a compilation error because you can't use push_back() with a map.
OC
Octavio 1 year ago
The output should be option A, since the map will automatically handle the duplicate keys and only store unique values.
SC
Scot 1 year ago
↳ replying to Octavio
B) compilation error
SC
Scot 1 year ago
↳ replying to Octavio
B) compilation error
BR
Brock 1 year ago
I think the answer is B) compilation error.
0/2000 characters

Save Cancel