New Year Sale 2026! 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 2 Question 116 Discussion

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

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

#include

#include

#include

using namespace std;

int main(){

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

string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight"," ten"};

map m;

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

m.insert(pair(second[i],first[i]));

}

if (m[11] == "eleven") {

cout<<"eleven ";

}

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

cout<second<<" ";

}

cout<

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

0/2000 characters
Rebecka
1 day ago
The last output should be 10, not 11!
upvoted 0 times
...
Alishia
7 days ago
Totally agree, it should just output the numbers.
upvoted 0 times
...
Dona
12 days ago
Wait, why would it output "eleven"?
upvoted 0 times
...
Afton
17 days ago
I think it will throw a runtime exception.
upvoted 0 times
...
Hyman
22 days ago
The map will have 10 elements.
upvoted 0 times
...
Marguerita
27 days ago
I'm going with Option D. The program will output "eleven one two three four five six seven eight nine ten 10" because the map will attempt to access an element with key 11, which is not present in the map, and the default value for that key will be "eleven".
upvoted 0 times
...
Lavonna
1 month ago
Option E is the correct answer. The program will throw a runtime exception because the map will attempt to access an element with key 11, which is not present in the map.
upvoted 0 times
...
Eden
1 month ago
Option D is correct. The program will output "eleven one two three four five six seven eight nine ten 10" because the map will attempt to access an element with key 11, which is not present in the map, and the default value for that key will be "eleven".
upvoted 0 times
...
Viva
1 month ago
I practiced a similar question, and I think the output will include the size of the map at the end, but I can't recall the exact values.
upvoted 0 times
...
Alica
2 months ago
I'm leaning towards option B or C. The output seems to be printing the values in the map, and the size of the map is 10, so that makes sense to me.
upvoted 0 times
...
Wendell
2 months ago
I think the key here is understanding how the map data structure works. If the index 11 doesn't exist, it might throw a runtime exception. I'll need to double-check that.
upvoted 0 times
...
Carmela
2 months ago
Okay, let me break this down. The code is creating two parallel arrays, then inserting the key-value pairs into a map. The last part is checking for a specific value at index 11, which seems odd.
upvoted 0 times
...
Ivan
2 months ago
The loop for printing the map seems off. I feel like there’s a typo with `i?>second`.
upvoted 0 times
...
Britt
2 months ago
The program will output "eleven one two three four five six seven eight nine ten 10" because the map will attempt to access an element with key 11, which is not present in the map, and the default value for that key will be "eleven".
upvoted 0 times
...
Margot
2 months ago
I remember something about accessing a non-existent key in a map. Could it throw an exception?
upvoted 0 times
...
Jerry
3 months ago
I think the map will insert the pairs correctly, but I'm not sure about the output after that.
upvoted 0 times
...
Paola
3 months ago
Haha, the programmer must have been a bit too ambitious with that map. Trying to access a key that's not even in the map? That's a recipe for a runtime exception, if you ask me.
upvoted 0 times
...
Tonja
3 months ago
Hmm, I'm a bit confused about the `m[11]` part. Does that mean it's trying to access an index that doesn't exist in the map?
upvoted 0 times
...
Teddy
3 months ago
This looks like a tricky one. I'll need to carefully read through the code and think through the logic step-by-step.
upvoted 0 times
...

Save Cancel