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 6 Question 105 Discussion

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

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

#include

#include

#include

using namespace std;

void myfunction(pair i) {

cout << " " << i.first;

}

int main() {

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

map m;

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

m[i]=t[i];

}

for_each(m.begin(), m.end(), myfunction);

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Una
19 days ago
Hold on, is that a typo in the code? 'pair' instead of 'pair'? Clearly, the answer is E. Someone call the compiler police!
upvoted 0 times
...
Pamela
22 days ago
B, definitely. Though I have to say, the real question is why anyone would want to do this in the first place. Seems like a lot of work just to print an array in order.
upvoted 0 times
...
Carmelina
23 days ago
E. Compilation error? Psh, please. This code is as clean as a whistle. I'm going with A. Just a good ol' fashioned array printing, nothing to see here.
upvoted 0 times
...
Jules
25 days ago
Ah, the old 'map the array to a map and then print the keys' trick. Clever, but I'm gonna go with C. Gotta keep those numbers in reverse order, you know.
upvoted 0 times
Jacqueline
20 days ago
I think it's A, not C. The keys are printed in order, not reverse.
upvoted 0 times
...
...
Lawrence
1 months ago
The correct answer is B. The code iterates through the map and prints the keys, which are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
upvoted 0 times
Ronald
24 hours ago
E) compilation error
upvoted 0 times
...
Johana
2 days ago
D) 1 3 8 7 4 2 6 9 5 10
upvoted 0 times
...
Aron
8 days ago
C) 9 8 7 6 5 4 3 2 1 0
upvoted 0 times
...
Hildred
18 days ago
B) 0 1 2 3 4 5 6 7 8 9
upvoted 0 times
...
Joanna
21 days ago
B) 0 1 2 3 4 5 6 7 8 9
upvoted 0 times
...
Brice
23 days ago
A) 10 5 9 6 2 4 7 8 3 1
upvoted 0 times
...
Mona
25 days ago
A) 10 5 9 6 2 4 7 8 3 1
upvoted 0 times
...
...
Dulce
2 months ago
I'm not sure about that. I think there might be a compilation error because the myfunction is expecting a pair but the map contains pairs of int, int. So, the correct answer might be E) compilation error.
upvoted 0 times
...
Reiko
2 months ago
I agree with Cristy. The code is iterating over the map and calling the myfunction for each pair, which will output the keys in ascending order.
upvoted 0 times
...
Cristy
2 months ago
I think the output will be B) 0 1 2 3 4 5 6 7 8 9 because the code is using a map to store key-value pairs and then iterating over it using for_each.
upvoted 0 times
...

Save Cancel